CLUSTER AND MULTICLUSTERS IN POSTGRESQL:
In PostgreSQL Before you can start a PostgreSQL server, you need to create a cluster. A cluster refers to a collection of databases managed by a single instance of the PostgreSQL server.
When you install PostgreSQL, It doesn’t automatically create a cluster. You have to initialize a cluster first, which involves following setup.
1. Firstly, creation of a directory. The cluster directory typically resides in a designated location on your system. Here run the commands to create a directory and transfer owner privileges to ‘Postgres’ user.
1 2 |
[root@postgres1 u01]#mkdir –p /u01/data [root@postgres1 u01]#chown –R postgres:postgres /u01/data |
2. Switch to the Postgres user to create the cluster. Run the following command:
1 2 |
[root@postgres1 u01]#su – postgres [postgres@postgres1 ~]$ |
3. Use the ‘initdb command, which initializes the data directory and essential system files for the PostgreSQL server. The initdb command also sets up the default template database, configuration files, and other necessary components. Run the following command:
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/initdb -D /u01/data1 |
Here, PostgreSQL creates the necessary files and directories within the specified location. i.e., /u01/data. Verify files in the data directory:
1 2 3 |
[postgres@postgres1 ~]$ cd /u01/data [postgres@postgres1 data]$ ls base pg_ident.conf pg_serial pg_tblspc postgresql.auto.conf global pg_logical pg_snapshots pg_twophase postgresql.conf pg_commit_ts pg_multixact pg_stat PG_VERSION postmaster.opts pg_dynshmem pg_notify pg_stat_tmp pg_wal pg_hba.conf pg_replslot pg_subtrans pg_xact |
Customize the PostgreSQL configuration files, such as postgresql.conf and
pg_hba.conf, based on your specific requirements.
4. Start the PostgreSQL server using the ‘pg_ctl’ command, which is used to start, stop, or check the status of the cluster. Run following command to start the cluster:
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/pg_ctl -D /u01/data1 start |
5. To list running processes, run the command ‘ps -ef | grep postgres, it will display a list of processes related to PostgreSQL. The ‘grep command filters the output of ‘ps -ef and only shows the lines containing the keyword “postgres”. This is useful for Identifying PostgreSQL-related processes running on the system.
1 2 3 4 5 6 7 8 9 10 |
[postgres@postgres1 ~]$ ps -efa|grep postgres root 3655 3583 0 15:22 pts/1 00:00:00 su - postgres postgres 3656 3655 0 15:22 pts/1 00:00:00 -bash postgres 3894 1 0 15:34 pts/1 00:00:00 /usr/local/pgsql/bin/postgres -D /u01/data1 postgres 3896 3894 0 15:34 ? 00:00:00 postgres: checkpointer postgres 3897 3894 0 15:34 ? 00:00:00 postgres: background writer postgres 3898 3894 0 15:34 ? 00:00:00 postgres: walwriter postgres 3899 3894 0 15:34 ? 00:00:00 postgres: autovacuum launcher postgres 3900 3894 0 15:34 ? 00:00:00 postgres: stats collector postgres 3901 3894 0 15:34 ? 00:00:00 postgres: logical replication launcher |
After the cluster is created and the server is started, you can connect to the you can connect to server using a client application, such as ‘psq1, and begin creating databases, tables, and performing various database operations.
MULTICLUSTER CONFIGURATION:
In PostgreSQL, each cluster is associated with a specific PostgreSQL version and runs on a specific port. Here we run the multiple PostgreSQL instances on the same machine, you need to create separate clusters for each instance, ensuring they use different ports and data directories.
1. Firstly, create the new data_directory as shown above. (/u01/data1)
2. Run the ‘initdb’ command to intialize the cluster
3. Customize the PostgreSQL configuration file ‘postgresql.conf’i.e changing the port number from default port number (5432) to different port number (5433). By doing the following steps :
1 2 3 4 5 6 |
[postgres@postgres1 u01]$ cd /u01/data1 [postgres@postgres1 data]$ vi postgresql.conf :/port – To search the port Press i to --insert-- Remove # and change the port (5233) :wq! – save & Exit |
1 |
4. [postgres@postgres1 ~]$ /usr/local/pgsql/bin/pg_ctl -D /u01/data start |
5. Run below command to verify the list of active processes.
1 |
‘ps -efa|grep postgres’ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[postgres@postgres1 ~]$ ps -efa|grep postgres root 3655 3583 0 15:22 pts/1 00:00:00 su - postgres postgres 3656 3655 0 15:22 pts/1 00:00:00 -bash postgres 3894 1 0 15:34 pts/1 00:00:00 /usr/local/pgsql/bin/postgres -D /u01/data1 postgres 3896 3894 0 15:34 ? 00:00:00 postgres: checkpointer postgres 3897 3894 0 15:34 ? 00:00:00 postgres: background writer postgres 3898 3894 0 15:34 ? 00:00:00 postgres: walwriter postgres 3899 3894 0 15:34 ? 00:00:00 postgres: autovacuum launcher postgres 3900 3894 0 15:34 ? 00:00:00 postgres: stats collector postgres 3901 3894 0 15:34 ? 00:00:00 postgres: logical replication launcher postgres 3960 1 0 15:39 pts/1 00:00:00 /usr/local/pgsql/bin/postgres -D /u01/data postgres 3962 3960 0 15:39 ? 00:00:00 postgres: checkpointer postgres 3963 3960 0 15:39 ? 00:00:00 postgres: background writer postgres 3964 3960 0 15:39 ? 00:00:00 postgres: walwriter postgres 3965 3960 0 15:39 ? 00:00:00 postgres: autovacuum launcher postgres 3966 3960 0 15:39 ? 00:00:00 postgres: stats collector postgres 3967 3960 0 15:39 ? 00:00:00 postgres: logical replication launcher root 5651 3783 0 17:03 pts/1 00:00:00 su - postgres postgres 5652 5651 0 17:03 pts/1 00:00:00 -bash postgres 6213 5652 3 19:22 pts/1 00:00:00 ps -efa postgres 6214 5652 0 19:22 pts/1 00:00:00 grep postgres |
6. To connect to the particular psql application of a cluster. Run the command with the port number.
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/psql -q 5433 |
Author : Prudhvi Teja |