Introduction to Postgres List Schemas
The database administrator and manager need to be aware of all the environment of the database server they are using. In the case of PostgreSQL, the server can handle data of 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. In this article, we will learn how we can check and view the schema related information in PostgreSQL.
We will begin with 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 the specific information and are modeled after the implementation for PostgreSQL.The schema is accessible to the first user of the database and has all the privileges to handle and operate on schema including dropping it.
Datatypes – Postgres List Schemas
The views of information schema use a special type of datatypes which are simple domains that are 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, in cases where the information is fetched from information schema then 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 has any maximum length specified which is 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 which are short and precise and can be used with the help of psql. Psql evaluates these metacommands and even translates them to SQL raw commands sometimes provided if they issued in the system tables of the server. Metacommands are recognized by a backslash 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 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 –
In any PostgreSQL database, there are three types of schemas present in it which are 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. To list out all the user-related schemas, we will create a query to retrieve from pg_catalog schema.
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 all schemas are owned by the user with id 10. To get the information about the user with id 10 we can query on pg_user table.
SELECT * FROM pg_catalog.pg_user where usesysid = 10;
Now we can say that user with usesysid =10 is postgres which owns all the above-retrieved schemas. Now, if we want to retrieve name os schema and the person owning it in a single output then 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 in a faster and effective manner 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 –