Skip to content

RootDBA

DBA expertise: PostgreSQL, SQL Server & MySQL

Menu
  • Home
  • blog
  • PostgreSql
  • MySql
  • Sql Server
  • About AHMED – Root DBA
Menu

database cluster in PostgreSql

Posted on September 1, 2025September 1, 2025 by AHMED

what is database cluster

database cluster is collection of database managed by single instance
don’t get confused by cluster that tied up by high availability

initdb

initdb : is utility used to create database cluster
initdb create setup directory called (data directory ) where we store our data .

initdb need to be installed , and once installed you have to initialize storage area 

data directory path

typically the location of data directory in Linux will be 

/var/lib/pgsql/data/

initdb syntax

you have to execute initdb using the postgres user
syntax:
initdb -D /usr/var/lib/pgsql/main

pg_ctl -D /usr/var/lib/pgsql/main

-D this option refer to data directory
-W we can use this option to force the super user to provide password before initialize DB
before you start make sure add binary of postgreasql command
export PATH=/usr/lib/postgresql/13/bin/:$PATH

to test this we will create directory in root folder called /postgresql/data/

and we will assigned the permission 

chmod 770 /postgresql/data/

9fe6806a9c48901fcf4c8943b5b3d44f.png
the we will run the command 

bb51a5a99e3c327e54db9eac7487f5f9.png
if get this error means the initdb is not added in environment of OS

usually the binary are located at ls /usr/lib/postgresql/13/bin/
c0224ce344bfc26f7b36e0f3d0043f0b.png
to run the command you will have to put the full path of initdb binary
with follow up with the option and directory
make sure that command is not executed using the root user
0b1843b152c55293f4f2683125593117.png
if you ran to the below issue you also have to change the owner of the folder using chown
chown postgres:postgres /postgresql/data/
1c3afe506e1b2fb1f6659b02bc1983ba.png

then try to run the command again and it should execute correctly
/usr/lib/postgresql/13/bin/initdb -D /postgresql/data/
51069df1c92243e7f06affe3ef26ac62.png

now go to the file you created and you will see there files created on new directory
fdfc08ab30b1e4682bf64b03fdadf093.png

starting the cluster

once we created the cluster it wont start automatically , we need to start the cluster using the command
same issue as INITDB command you have to give the full path of the command or edit the environment to include the pg_ctl path
pg_ctl start
/usr/lib/postgresql/13/bin/initdb -D /postgresql/data/
if will ran to error as showing below , because the cluster is using the same port 5432 as default cluster
4887513947b0a67ea1f513f537a728f4.png
you need to change it to random one
edit the postgresql.conf using nano and edit the port and enable listening to local host
9c701f349d78138961792f7498f56530.png
now if you ran the command again it will execute successfully 

how to connect to the cluster

to connect to the new cluster we have created use the below syntax , and make sure to provide the correct port for the cluster 

psql -U postgres [or any user you want] -p 5444[port number]
545ae94a9110712e0828b508bab50bcf.png\q

how to check the status of cluster

you can check if the cluster is runinng by using the below command
/usr/lib/postgresql/13/bin/pg_ctl status -D /postgresql/data/

shutdown option

below command is related to shutdown the cluster
1- smart : the server disallow new connection , but the existent session work normally , its shutdown only after all session terminated.
2- fast (default) : the server disallow new connection and abort their current transaction and exit gracefully.
3-  immediate : quits/abort without proper shutdown which lead to recovery on next startup 

syntax

for the propose i will use smart option in shutdown the new cluster we have created , for the rest of shutdown option is basically the same .

pg_ctl stope -m smart[also you can uses immediate or fast] -D[data directory]
5082bb521c3d8c283a7f1972ca58333b.png

reload vs restart

reload is used when we did some changes on configuration files to , reload only load new config with restarting the services .
Some changes in config wont reflect unless we restart the services
restart : gracefully shutdown all activity , close all open files and start again with new configuration .

reload & restart syntax

reload : system reload postgresql-11
restart : systectl restart postgresql

pg_controldata

its used to provide information about cluster
such as version number , last checkpoint 

syntax

pg_contrldata /var/lib/pgsql/main

see data directory

to see the data directory connect psql client and use the below command 

show data_directory;

0
Visited 11 times, 1 visit(s) today
Category: PostgreSql, Postgresql Dba Guide

Post navigation

← Installing PostgreSQL on linux
PostgreSQL Directory layout →
© 2025 RootDBA | Powered by Minimalist Blog WordPress Theme