Saturday, October 22, 2022

create a new database in postgresql

Create database notes
1) A database can be created using
create database <database_name>;
2) To customize the database creation, we have several options like... template name, role_authorization, connection limits etc...
3) We need to make sure right lc_collate (support for sorting order), lc_ctype (support for case specification) are used while creating the db. By default if no attributes are provided, it will use the template1's attribute.
4) Let us take a look at list of dbs we have now....
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(3 rows)
postgres=#
5) We have 3 dbs, of which we know all this 3 are factory or system provided/created ones. Now we need to create one new db. For now let us not customize the locale settings like lc_collate, lc_ctype etc..
create database pgtst_db;
Output:

-bash-4.2$ psql -U pgtst_usr -d postgres
Password for user pgtst_usr:
psql (15.0)
Type "help" for help.
postgres=> \conninfo
You are connected to database "postgres" as user "pgtst_usr" via socket in "/var/run/postgresql" at port "5432".
postgres=>

postgres=> create database pgtst_db;
CREATE DATABASE

postgres=> \l
                                                  List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+-----------+----------+-------------+-------------+------------+-----------------+-----------------------
 pgtst_db  | pgtst_usr | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | <<< we see same enconding,local settings as template1.
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |           |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |           |          |             |             |            |                 | postgres=CTc/postgres
(4 rows)
postgres=>

The same can be achieved using the psql command below...
Query:

select a.oid
,a.datname
,b.rolname owner
,a.encoding
,a.dattablespace
,a.datcollate
,a.datctype
,a.datacl
from pg_database a
,pg_authid b
where b.oid=a.datdba;

as postgres user executed the above command:

postgres=# select a.oid
postgres-# ,a.datname
postgres-# ,b.rolname owner
postgres-# ,a.encoding
postgres-# ,a.dattablespace
postgres-# ,a.datcollate
postgres-# ,a.datctype
postgres-# ,a.datacl
postgres-# from pg_database a
postgres-# ,pg_authid b
postgres-# where b.oid=a.datdba;
  oid  |  datname  |   owner   | encoding | dattablespace | datcollate  |  datctype   |               datacl
-------+-----------+-----------+----------+---------------+-------------+-------------+-----------------------------------
--
 16390 | pgtst_db  | pgtst_usr |        6 |          1663 | en_US.UTF-8 | en_US.UTF-8 |
     4 | template0 | postgres  |        6 |          1663 | en_US.UTF-8 | en_US.UTF-8 | {=c/postgres,postgres=CTc/postgres
}
     1 | template1 | postgres  |        6 |          1663 | en_US.UTF-8 | en_US.UTF-8 | {=c/postgres,postgres=CTc/postgres
}
     5 | postgres  | postgres  |        6 |          1663 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
postgres=#

Thanks
postgres=# \l

No comments:

Post a Comment

Flashback data archive steps

 Objective: Ways to track DML changes in a table Detailed objective: We should be able to track the changes in the table at any point in tim...