Sunday, October 23, 2022

Postgresql: Interacting with schemas

To get a list of schemas in a db, we can run the below query..

select * from pg_catalog.pg_namespace;

Actual output:

postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# select * from pg_catalog.pg_namespace;
  oid  |      nspname       | nspowner |                            nspacl
-------+--------------------+----------+---------------------------------------------------------------
    99 | pg_toast           |       10 |
    11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
  2200 | public             |     6171 | {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
 13915 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}
(4 rows)
postgres=#

postgres=# \dn
      List of schemas
  Name  |       Owner
--------+-------------------
 public | pg_database_owner
(1 row)

postgres=#

This result is coming from postgres database (default).
If we run the same command in another custom build db/schema...

pgtst_db=> \c
You are now connected to database "pgtst_db" as user "pgtst_usr".
pgtst_db=>

pgtst_db=> select * from pg_catalog.pg_namespace;
  oid  |      nspname       | nspowner |                            nspacl
-------+--------------------+----------+---------------------------------------------------------------
    99 | pg_toast           |       10 |
    11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
  2200 | public             |     6171 | {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
 13915 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}
 24582 | pgtst_schema       |    16388 |
(5 rows)
pgtst_db=>

pgtst_db=> \dn
         List of schemas
     Name     |       Owner
--------------+-------------------
 pgtst_schema | pgtst_usr
 public       | pg_database_owner
(2 rows)

pgtst_db=>


So we learn, the custom build DB is a copy of template 1 and hence it is reporting those default 4 schemas which forms postgresql dictionary.
Also the \dn command hides the postgresql dictionary to avoid hopefully the end user interaction.

Thanks

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...