Wednesday, 14 August 2024

What is The Use of Initdb in PostgreSQL?

 

What is The Use of Initdb in PostgreSQL?

It will create a new PostgreSQL cluster.
In Postgres cluster is a collection of databases that are managed by a single cluster.
It will create the data directories in which the database data will live, generating the shared catalog tables and creating default databases.
* Postgresql — used for connections

* template 0 — user connection is not allowed and maintain DB consistency

* template 1 — When we creating a database will take an exact copy of template1.

In case the user doesn’t have permission on the data directory will create an empty directory.
initdb initializes the database cluster with default locale and character set encoding. This character set encoding, collation order (LC_COLLATE) and character set classes (LC_CTYPE, e.g., upper, lower, digit) can be set separately for a database when it is created. initdb determines those settings for the template1 database, which will serve as the default for all other databases.
Before the Initdb initialises, the data directory was empty.
[postgres@prime]$cd /u01/pgdatabase/data
[postgres@prime data]$ ls -lrth
total of 0
Now we are going to execute the initdb -D “ path ”, here -D specifies data directory location
postgres@prime data]$ /u01/postgresql-10.0/bin/initdb -D /u01/pgdatabase/data
Once the initdb initialised, to verify the data directory.
[postgres@prime data]$ ls -lrth
total 124K
-rw------- 1 postgres postgres 88 Oct 30 2019 postgresql.auto.conf
-rw------- 1 postgres postgres 3 Oct 30 2019 PG_VERSION
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_twophase
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_tblspc
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_snapshots
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_serial
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_replslot
drwx------ 4 postgres postgres 4.0K Oct 30 2019 pg_multixact
-rw------- 1 postgres postgres 1.6K Oct 30 2019 pg_ident.conf
-rw------- 1 postgres postgres 4.5K Oct 30 2019 pg_hba.conf
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_dynshmem
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_commit_ts
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_xact
drwx------ 2 postgres postgres 4.0K Oct 30 2019 pg_subtrans
-rw------- 1 postgres postgres 23K Feb 14 22:17 postgresql.conf
drwx------ 11 postgres postgres 4.0K Feb 14 22:43 base
drwx------ 2 postgres postgres 4.0K Feb 14 22:44 pg_stat
-rw------- 1 postgres postgres 769 Feb 14 22:46 logfile
drwx------ 3 postgres postgres 4.0K Mar 2 11:40 pg_wal
-rw------- 1 postgres postgres 62 Mar 7 13:38 postmaster.opts
drwx------ 2 postgres postgres 4.0K Mar 7 13:38 pg_notify
-rw------- 1 postgres postgres 78 Mar 7 13:38 postmaster.pid
drwx------ 2 postgres postgres 4.0K Mar 7 13:46 global
drwx------ 4 postgres postgres 4.0K Mar 7 15:43 pg_logical
drwx------ 2 postgres postgres 4.0K Mar 8 11:33 pg_stat_tmp
Here the important files is created automatically i.e postgresql.conf,pg_hba.conf,base,pg_tblspc ..etc


Summary:

The initdb use to create the data directories and default databases as well. By using initdb we can create multiple clusters on the same machine with the different directory path.

No comments:

Post a Comment

Master and Slave - Sync check - PostgreSQL

  1) Run the below Query on Primary:- SELECT     pid,     usename,     application_name,     client_addr,     state,     sync_state,     sen...