Create Database in PostgreSQL DB
You can create a database in PostgreSQL with "CREATE DATABASE <database_name>" PSQL command. You also can use operating system level utility "createdb <database_name>" to create the new database
When you install PostgreSQL and initialize the PostgreSQL DB cluster, PostgreSQL created 2 template database with names template0 and template1. This can be confirmed from the below output.
postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(5 rows)
postgres=# create database pgdb3;
CREATE DATABASE
postgres=#
postgres=# \q
[postgres@olinux8 ~]$
[postgres@olinux8 ~]$ createdb pgdb4
[postgres@olinux8 ~]$ psql -h localhost -U postgres -p 5432
Password for user postgres:
psql (18.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb4 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(7 rows)
postgres=#
Whenever a new database is created, the template database template1 is cloned, by default. So, if you make any changes to template1 database, then those changes will be propagated to all the future databases.
template0 database is the gold image of the template1 database without any modification. So, if you want to create a new database, but do not want any custom changes to be propagated from template1 database, then you can clone template0 database to create your new database, using the below command.
PSQL syntax:
CREATE DATABASE <database_name> TEMPLATE template0;
OS utility:
$ created -T template0 <database_name>
postgres=# CREATE DATABASE pgdb5 TEMPLATE template0;
CREATE DATABASE
postgres=#
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb4 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb5 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(8 rows)
postgres=# exit
[postgres@olinux8 ~]$ createdb -T template0 pgdb6
[postgres@olinux8 ~]$
[postgres@olinux8 ~]$ psql -h localhost -U postgres -p 5432
Password for user postgres:
psql (18.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb4 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb5 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb6 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(9 rows)
postgres=#
CREATE DATABASE
postgres=#
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb4 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb5 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(8 rows)
postgres=# exit
[postgres@olinux8 ~]$ createdb -T template0 pgdb6
[postgres@olinux8 ~]$
[postgres@olinux8 ~]$ psql -h localhost -U postgres -p 5432
Password for user postgres:
psql (18.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
pgdb1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb4 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb5 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
pgdb6 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(9 rows)
postgres=#