How to create a database in postgresql

PostgreSQL simplified database creation, once initialized the databases cluster by default it create 3 database, postgres , templete0 and template1.

If you don't create any new database, all the objects will be created under default postgres database.


Follow below steps to create new database in postgresql.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)


- To create a new database.

postgres=# create database dbnew;
CREATE DATABASE

( Database use default tablespace pg_default.)

- To create a new database with user defained tablespace.

First create new tablespace.

postgres=# create tablespace new_ts location '/app01/new_ts';
CREATE TABLESPACE

postgres=# create database dbnewts with tablespace new_ts;
CREATE DATABASE


postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 dbnew     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 dbnewts   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(5 rows)

0 comments:

Post a Comment