P0020 Clusters, Databases, Schemas, Files and Processes
Clusters
![]() | Clusters are basic building blocks of PostgreSQL. Users, databases and tablespaces are independent object defined on the cluster level. Each cluster can have multiple databases. Databases are independent, but each of them can access global cluster objects ( Users and Tablespaces ). Databases are divided into schemas. Inside of the schemas we will find tables, functions, indexes, sequences and other database objects. |
sudo pg_createcluster 17 testCluster | In the previous article we saw how to create a cluster. I will create cluster "testCluster". |
sudo pg_ctlcluster 17 testCluster start | In the previous article we learned how to start a cluster. |
![]() | We can check existence and status of our new cluster with:pg_lsclusters. |
sudo pg_ctlcluster 17 testCluster reload                                                                                         | After the change of some configuration settings, we have to reload or restart our cluster. We do it like this. |
sudo systemctl reload postgresql | It is possible to reload or restart all of the clusters at once, by using systemctl reload/restart. |
It is possible to rename cluster. Before that, we have to stop the cluster. sudo pg_ctlcluster 17 testCluster stop | ![]() |
![]() | Our cluster now has a new name. We will use the new name to delete this cluster.sudo pg_dropcluster 17 newCluster |
Closing Cluster Mode
sudo pg_ctlcluster 17 main stop | This command by default is using "smart" mode. There are 3 modes: smart, fast and immediate. |
sudo pg_ctlcluster 17 main stop -m smart                                                                                          | This is "smart" mode when we want to be explicit. Server will wait for all of the users to end their sessions and to disconnect. New connections are not allowed. If one of the users don't disconnect, then the server will not shut down. This is good for disciplined users. |
sudo pg_ctlcluster 17 main stop -m fast | Fast mode means that we will end all of the transactions, we will rollback all of the changes, and then we will disconnect users. The server will close fast and clean. |
sudo pg_ctlcluster 17 main stop -m immediate | "Immediately" means that all users will be disconnected immediately. The server will shut down in a "dirty" manner. After restarting, recovery from WAL files will be required. This should only be used in emergencies when the server freezes or other methods do not work. |
I will stop 17/main cluster. Â sudo pg_ctlcluster 17 main stop -m fast | ![]() | Immediatelly after, I will start the cluster again.sudo pg_ctlcluster 17 main start |
Problem With the Postgres User
This time I will make a mistake. I will take the role of the postgres user and I will stop the cluster by using "pg_ctlcluster…stop" command. sudo -i -u postgres pg_ctlcluster 17 main stop | This time we will get a message to use systemctl. ![]() |
Closing the server with "pg_ctlcluster" command, while being "postgres" user, will not corrupt your server. The message just means that we have stopped the cluster behind the back of the systemd. This will force systemd to log that the process has failed. The problem is that postgres user has authority over PostgreSQL, but can not communicate with systemd. Only the root can do both.
![]() | Although we deliberately closed the cluster, if we check the status of this process ( as an admin ), we will see that it "failed". sudo systemctl status postgresql@17-main |
sudo systemctl start postgresql@17-main | We can start/stop our cluster using canonical approach with systemctl. I will start and stop the cluster and then we will check its status. |
![]() | Now the systemd is happy and will correctly label our process as inactive (dead). |
sudo systemctl start postgresql@17-main #first we have to start it sudo systemctl reload postgresql@17-main | This also means that it is possible to use systemctl to reload or restart a specific cluster. |
postgresql vs postgresql@17-main
Systemd is Linux subsystem used to control services. The name of PostgreSQL service is "postgresql" ( postgresql.service ). This service has a configuration file with the name postgresql@.service that is at the location "/usr/lib/system/system/postgresql@.service". This is the so-called systemd unit file.
sudo systemctl reload postgresql | We can reload all of the PostgreSQL clusters, or we can reload just the specific one. |
The relationship between "postgresql" and "postgresql@17-main" is template-instance ( parent-child ). "postgresql" is the base service, which has a systemd unit file template. When we start a new cluster, systemd will create an instance based on that template. That is why "postgresql" service ( parent ) will have "postgresql@17-main" and "postgresql@18-main" children. So, we can control all of the child services together (postgresql ), or we can control them separately ( "postgresql@17-main" and "postgresql@18-main" ).
Problem With the Postgres User Part II
I will show you one more problem that can happen with the "postgres" user.
sudo -i -u postgres | I will become "postgres" user and I will stop and start 17/main cluster. Notice the messages I am getting. ![]() |
exit                                                                                 | This time I will exit, and I will again be admin. I will stop/start the cluster with systemctl. ![]() I will get this error message: "Job for postgresql@17-amin.service failed because the service did not take the steps required by its unit configuration." |
Now we have the real problem. "systemctl" and "pg_ctlcluster" are out of sync. We have to sync them. Do these steps as an admin.
sudo pg_ctlcluster 17 main stop | This third step is probably not needed, but do it just anyway. We have solved the problem.![]() |
Conclusions
Do not use "initdb" and "pg_ctl" on Ubuntu. We talk about that in the previous article.
Do not control "postgresql" service as a "postgres" user. Work with services is only for the administrator.
Databases
sudo -u postgres psql -p 5432 -d postgres | I will login to database postgres as the user postgres. | |
If we type "\l", we will get the list of all of the databases in that cluster. There are three bult-in databases. | ![]() | |
SELECT datname, oid FROM pg_database; | We can read from the system table "pg_database". Inside of it we will find the three builtin databases and their ID-s. We can find those databases in the "base" folder in the data directory. Every newly created database will be placed there. ![]() | |
Command "\conninfo" will list data about the current connection. We can see that we are attached to the database "postgres". Because we are admin we can easily switch to other database "\c template1". In this way we can jump to other databases, but not to " template0" database. | ![]() | |
Postgres Database
"postgres" database is metadata database. Inside of it we store info about users, because users are defined on the cluster level. Extensions save their data into "postgres" database. You should never store user data into this database. This database is just for metadata and administration.
Template Databases
Creation of the new databases is based on duplication of the existing database. We can not duplicate database where someone is connected to it. I will log-in once more as a "postgres" user. I am now logged twice as a "postgres" user, in the separate terminal tabs.
#NEW TAB sudo -u postgres psql -p 5432 -d postgres  This query will last for ten seconds. During that time, I will try to create a database in the other tab. SELECT pg_sleep(10); | Creation of the database will fail, because of the session in the new tab. #OLD TAB CREATE DATABASE baza TEMPLATE postgres; |
Normal way of database creation is to duplicate embedded "template" database. There are two such databases. Database "template0" is the most restricted database. This database can only be used to create new databases and nothing else. This is a "sacred" PostgreSQL database. We can not change it in any way.
Database "template1" is the database that all the new databases will be based on by default. We can modify this database to suit our needs.
I will now create a table inside of the "template1" database. CREATE TABLE templateTable ( Col1 INT ); | |
I will then jump into "postgres" database and I will create a new database. \c postgres  #"template1" is default template. | ![]() |
When we log into this new database, we will find a table already there. \c baza | ![]() |
How to Use Template Databases?
We can use any other database as a template. I will create another database "baza2" from the "baza" database. CREATE DATABASE baza2 TEMPLATE baza; \c baza2SELECT * FROM templateTable; We can use any database as a template to create new databases. | ![]() | |
![]() | SELECT oid, datname, datistemplate FROM pg_database; In the system table " pg_database" we can see all of the databases. Template databases are labeled with the letter "t" in the column "datistemplate". Â Â |
In the start, databases "template1" and "template0" are the same. We changed that by changing "template1". I will show you how you can reset "template1" to its vanilla state. We can not delete template databases, so we have first to remove template=TRUE attribute.
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1'; DROP DATABASE template1; #we will recreate "template1" database based on the "template0" database. CREATE DATABASE template1 TEMPLATE template0; | ![]() |
How to Administer Template Databases?
We should never make direct changes to "pg_database" system table. Instead of that we should use ALTER DATABASE statement.
I will jump to "postgres" database. I will make the database "baza2" a template. \c postgres ALTER DATABASE baza2 IS_TEMPLATE true; The difference is that now we can not delete " baza2", because it is a template. I will remove template property from this database and I will delete it. | ![]() ALTER DATABASE baza2 IS_TEMPLATE false; |
Schemas
![]() | Schemas are organizational units inside of the database. Every table, view, index must belong to some schema. We have one table in the database "baza". \c baza      #we jump to that database\dt       #this will list all of the tables![]() | We can list all of the tables with the "\dt" command. We can see that by default; a table will belong to "public" schema.Similar to that, we can list all of the available schemas with the " \dn".![]() |
SELECT oid, nspname                                       | Command "\dn" will only show user visible schema. If we want to see all of the schema then we should read from the "pg_namespace" system table. – information_schema has set of read-only standardized metadata views. – pg_catalog schema holds system tables. – pt_toast is used to store very big pieces of data. | ![]() |
It is also possible to observe tables from all of the schemas. We can do that in three ways:
![]() | Native PostgreSQL query used to find out about tables is called "pg_tables". This query has less columns and information than the information_schema.tables query. | SELECT schemaname, tablename                                                       |
SELECT table_schema, table_name                                                            | There is a standard how to read metadata. That standard is based on standardized view. Such views are called "information_schema" and many different database servers have them ( Oracle, Snowflake… ). | ![]() |
![]() | Both queries are pulling data from the system table "pg_class". In this table, the column "realnamespace" has ID of the schema and this is connection with the "pg_namespace" table ( 2200 for "public" schema ). | SELECT relnamespace, relname |
"pg_catalog" Schema
This schema stores important tables that contain database metadata. These are some of those tables and they store metadata about:
pg_class – database tables. | pg_attribute – table columns. | pg_proc – procedures and functions. | pg_type – data types. | pg_index – indexes. |
Creation of the Custom Schema
I will create new schema. CREATE SCHEMA myschema; | We can rename our schema.ALTER SCHEMA myschema RENAME TO newname; | \dn   #read our schemas ![]() |
CREATE TABLE newname.templateTable ( ColA INT ); | When we want to create new table inside of this schema, we should provide a schema name. |
SELECT Col1 FROM public.templateTable; | We now have two tables with the same name – "templateTable". They will not clash, because each of them is in the separate schema. We just have to call them by their full qualified names. |
SELECT * FROM templateTable;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | If we run a statement like this, which doesn't use a fully qualified names, we will get the table from the "public" schema. There is a system how PostgreSQL resolve these situations, but that is the story we will talk later. |
Moving Tables Between Schemas
I want to move both tables in the "public" schema, and to delete "newname" schema. First, I have to rename the table from the "newname" schema. | ALTER TABLE newname.templateTable RENAME to tT; After that, we can change the schema of our table: ALTER TABLE newName.tT SET SCHEMA public;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
Let's see what tables are now inside of the "public" schema. Â SELECT schemaname, tablename FROM pg_tables WHERE schemaname = 'public'; | Â |
There is no more a table inside of the "newname" schema. We can delete it. DROP SCHEMA newname; \dn  tells us that "newname" schema has been deleted. | ![]() |
PostgreSQL File Structure
We will exit from psql with "exit". PostgreSQL binary files are places inside of the "/usr/lib/postgresql" directory.Inside of it we can see separate folders for the server versions 17 and 18. | ![]() |
Cluster files are placed in the two folders. The first one contains the cluster files, and the second one contains configuration files. I will list here some important files. We will first become a root with "sudo -i". You can check the content of these files on your computer. | /var/lib/postgresql/18/main # data directory                                                                                               |
cat /var/lib/postgresql/18/main/PG_VERSION | This will return the version number of PostgreSQL. It will be number "18". |
cat /var/lib/postgresql/18/main/postmaster.opts | If we start a server with some command line options, those will be written here. |
cat /var/lib/postgresql/18/main/postmaster.pid | Here we can find process ID, data directory ( where the cluster is ), timestamp when the server started to run ( unix timestamp ), port number, local or internet access. |
cat /etc/postgresql/18/main/pg_hba.conf | "hba" means host-based authentication. Here we configure authorization and authentication. |
cat /etc/postgresql/18/main/pg_ident.conf | Here we can map Linux user to PostgreSQL user. This mapping must be referenced in the "pg_hba.conf" file to allow local user on local computer to log-in. Can not be used for remote log-in. |
cat /etc/postgresql/18/main/postgresql.conf | This is file with most of the configuration settings for a cluster. |
cat /etc/postgresql/18/main/postgresql.auto.conf                                                                                                | We can use ALTER SYSTEM command directly from the psql to change some settings. New values will be written here. Settings from here will override settings in the postgres.conf. This file should not be modified manually. |
PostgreSQL Processes
In the separate terminal tab, I will log-in into 18/main cluster. sudo -u postgres psql -p 5432 -d postgres  In the original terminal tab, I will stop cluster 17. sudo pg_ctlcluster 17 main stop   | I will run this command. This command will list all of the process that are started by the "postgres" executable. " -a" means that we will look at processes of all of the users. " -f" will give us a lot of data.  ps -C postgres -af |
![]() | We will get all the important PostgreSQL processes. – The longest line is our "postgres" daemon. – Checkpointer is for writing dirty data. – Background writer – helper to checkpointer. – WAL writer – writing clean data to disk. – Autovacuum – some kind of garabage collector. – Logical replication – used for database replication. Every logged user will have his own process. One such process is the third process from bellow. |




























 






































This is the response that we will get. "
For this certificate we will see that the certificate has been revoked.
We will get interactive prompt.

SAN is the only extension.


















