How to Create a New Database in PostgreSQL Database Server via PostgreSQL Command Console

Posted on

This article will show how to create a new database in PostgreSQL Database Server. It is a very simple task where the process exist in PostgreSQL command console. In order to create a new database in PostgreSQL Database Server, there are several things as a prerequisite available. Those prerequisite are :

1. The PostgreSQL Database Server is available and it is currently running. Just read the article with the title of ‘How to Check PostgreSQL Database Server’s service status. ‘ in this link. Moreover, check all the available PostgreSQL articles in this link for further reference.

2. Access to the PostgreSQL command console using the following command :

psql -Uusername

For an example :

C:\Users\Personal>psql -Upostgres
Password for user postgres:
psql (12.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=# \q

The above example is the execution of accessing PostgreSQL command console in Windows operating system. But in the end of the day, the command is still the same. Also, the output is presenting the same PostgreSQL command console. Don’t forget to pass on the suitable password before accesing the PostgreSQL command console.

3. Execute the command to create a new database. The command pattern exist as follows :

create database database_name

The following is an example for creating a new database using the above command pattern :

postgres=# create database employee;
CREATE DATABASE
postgres=# 

3. Don’t forget to check whether the new database is exist or not after executing the above command. The command execution available in the article with the title of ‘How to List All Database in PostgreSQL via Command Line’ in this link. Execute the following command to list the available database :

\l

Execute the above query in the PostgreSQL command console to list the available databases as follows :

postgres=# \l
                                                  List of databases
    Name     |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-------------+----------+----------+----------------------------+----------------------------+-----------------------
 employee    | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres    | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0   | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
             |          |          |                            |                            | postgres=CTc/postgres
 template1   | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
             |          |          |                            |                            | postgres=CTc/postgres
(4 rows)


postgres=#

Apparently, according to the above command output, the new database with the name of ’employee’ exist.

Leave a Reply