Introduction to Postgres List Schemas
The database administrator and manager need to be aware of all the database server’s environment. In the case of PostgreSQL, the server can handle data from multiple databases at a single time. The database administrator needs to know about all the databases and the tables and schema related data about the database server. This article will learn how we can check and view the schema related information in PostgreSQL.
We will begin by knowing what schema is in the database. In any database, all the information about the objects being managed in it is stored in the collection of the views called schema. The information schema remains stable and is portable as it is defined inside the SQL standards. However, the system catalogs are variable as they contain specific information and are modelled after PostgreSQL implementation. The schema is accessible to the first user of the database and has all the privileges to handle and operate on the schema, including dropping it.
Datatypes – Postgres List Schemas
The views of information schema use a special type of data types that are simple domains generated over the built-in ones. These data types are not to be used in our outer work related to our databases. It is exclusively used by information schema view only. However, when the information is fetched from the information schema, you need to handle them in your application. All columns in information schema views can belong to one of the following datatypes.
|cardinal_number||It is a non-negative, i.e. positive integer|
|character_data||It specifies the string of characters that does not has any maximum length specified, which is used for fields containing data other than SQL identifiers.|
|sql_identifier||It specifies the string of characters that does not have any maximum length specified, used for SQL identifiers.|
|time_stamp||It is the domain defined over timestamp, which also considers time zones.|
|yes_or_no||It is the string of characters that can either have YES or NO value in it. It is kept for backward compatibility of information schema as information schema was created and came into usage even before the boolean data type was added in the SQL standards, which had either true/false value.|
Instead of using raw SQL queries for listing the data from the database, we can use metacommands that are short and precise and can be used with psql. Psql evaluates these metacommands and even translates them to SQL raw commands sometimes provided if they issued in the server’s system tables. A backslash recognizes metacommands and the command keyword followed by the parameters if any, if you want to pass to the query.
Most of the PostgreSQL servers have three default databases when they are created, which are namely template0, template1, and Postgres. Template0 and template1 are the basic databases that are internally used by the command CREATE DATABASE for its usage. These two databases are called skeleton databases. The default database which is SELECTed and shown to you is the Postgres database. After that, you can create databases of your choice and switch to them to create and manipulate tables in your databases. All databases can be retrieved and listed using the metacommand \list or \l and can be switched from one to other using \connect or \c. We can list out all the tables using the metacommand \dt command.
How to Use Postgres List Schemas?
Now, we will see how we can list databases using the psql command.\list, or \l can be used.
Open your PostgreSQL command prompt and then type SQL to get its command prompt. No type \list and press enter. These are the output that you will see “Three default databases of PostgreSQL”.
sudo su - postgres
Let us create one new database named demo by using the createdb command. Exit the psql by typing \q and then just type createdb demo and press enter. Then again, enter \list and press enter to list out all the databases after typing psql to get the command prompt of psql. The output will be as follows –
There are three types of schemas present in any PostgreSQL database: information schemas, temporary schemas, and default pg_* schemas. Other than the user-defined and public schemas, one more type of schema present in PostgreSQL is the pg_catalog schema. This schema contains the information about and the system tables, operators, data types and functions present in the current database. We will create a query to retrieve from the pg_catalog schema to list out all the user-related schemas.
SELECT * FROM pg_catalog.pg_namespace;
The field nspname displays the names of the schemas. These are all the schemas present in our database right now. The npsowner field stores the user id which owns that schema. We can see that the user with id 10 owns all schemas. To get the information about the user with id 10, we can query on the pg_user table.
SELECT * FROM pg_catalog.pg_user where usesysid = 10;
We can say that the user with usesysid =10 is postgres which owns all the above-retrieved schemas. If we want to retrieve the name os the schema and the person owning it in a single output, we can create a join on pg_namespace and pg_user as shown below.
SELECT s.nspname AS table_schema, u.usename AS OWNER FROM pg_catalog.pg_namespace s JOIN pg_catalog.pg_user u ON u.usesysid = s.nspowner ORDER BY table_schema;
PostgreSQL databases provide us with metacommands that are compact and immensely useful for database administrators and managers to check the database environments and structure related information faster and effectively during their daily routines. Schemas can be retrieved from the system tables of pg_catalog schema, which is present in PostgreSQL databases. pg_namespace and pg_user are two main tables that convey schema related information to us.
This is a guide to Postgres List Schemas. Here we discuss an introduction to Postgres List Schemas, with appropriate syntax, command and examples. You can also go through our other related articles to learn more –