postgres source code - ghdrako/doc_snipets GitHub Wiki

Installing PostgreSQL Server Using Source Code

Step 1: Create the required OS directories

  • Directory for staging PostgreSQL software – /usr/local/software
  • Installation directory for the PostgreSQL binaries – /var/postgres/postgres16.2
  • Location of the data directory – /var/pgdata/postgres16/dat
 [root@pg_server ~]# mkdir -p /usr/local/software
 [root@pg_server ~]# mkdir -p /var/postgres/postgres16.2
 [root@pg_server ~]# ls -lrt /usr/local/software
 [root@pg_server ~]# ls -lrt /var/postgres/postgres16.2

Step 2: Download the required source code for specific PostgreSQL version 16.2. We can download the source code for all the available versions from the URL below: https://ftp.postgresql.org/pub/source


[root@pg_server ~]# cd /usr/local/software
 [root@pg_server software]# wget https://ftp.postgresql.org/pub/source/
 v16.2/postgresql-16.2.tar.bz2 --no-check-certificate

Step 3: Extract the downloaded source code file.

 [root@pg_server software]# tar -xvf postgresql-16.2.tar.bz2
  • Configure - this command will check if all the required libraries are available or not
  • Make - Make command will export all the required library paths and ensures that the system is ready for installation
  • Make Install - Installs PostgreSQ

Step 4: Run the configure command

 [root@pg_server postgresql-16.2]# ./configure --help

If we want to go with the default installation path of “/usr/local/pgsql”, we can just run the “./configure” command without any options as root user

 ./configure --prefix=/var/postgres/postgres16.2/
 [root@pg_server postgresql-16.2]# yum install readline-devel
[root@pg_server postgresql-16.2]# yum install zlib-devel

As root user, we can now run the configure command with prefix option for non- default binary location.

 [root@pg_server postgresql-16.2]# ./configure --prefix=/var/postgres/
 postgres16.2

 [root@pg_server postgresql-16.2]# tail -10 config.log

Step 5: Building PostgreSQL from the source code with the “make” command

 [root@pg_server postgresql-16.2]# make

Step 6: Install PostgreSQL using the “make install” command

[root@pg_server postgresql-16.2]# make install

Step 7: Build and install contrib module using the make command.

 [root@pg_server ~]# cd /usr/local/software/postgresql-16.2/contrib
 [root@pg_server contrib]# make
[root@pg_server contrib]# make install

The information about build and installation of PostgreSQL instance can be checked using a utility, pg_config.

 [root@pg_server ~]# /var/postgres/postgres16.2/bin/pg_config

As root user, create a directory for data and change its ownership.

 [root@pg_server ~]# mkdir -p /var/pgdata/postgres16/data
 [root@pg_server ~]# chown -R postgres:postgres /var/pgdata/postgres16/data
 [root@pg_server ~]# chown -R postgres:postgres /var/postgres/postgres16.2

Step 8: Initialize the storage area/data directory in specific location. We will use the directory /var/pgdata/postgres16/data created earlier. As postgres user, run the below command to initialize data directory:

 [root@pg_server ~]# su - postgres
 [postgres@pg_server ~]$ /var/postgres/postgres16.2/bin/initdb -D /var/pgdata/postgres16/data
 [postgres@pg_server ~]$ cd /var/pgdata/postgres16/data
 [postgres@pg_server data]$ /var/postgres/postgres16.2/bin/pg_ctl -D /var/pgdata/postgres16/data status
[postgres@pg_server data]$ /var/postgres/postgres16.2/bin/pg_ctl -D /var/pgdata/postgres16/data start -l /tmp/server.log
 [postgres@pg_server data]$ /var/postgres/postgres16.2/bin/pg_ctl -D /var/pgdata/postgres16/data status

Step 9: Add the startup command in the rc.local script. To ensure the PostgreSQL server is automatically started on system boot, the pg_ctl startup command can be added to the rc.local script file. As root user, add the below line to the /etc/rc.d/rc.local file:

 su - postgres -c '/var/postgres/postgres16.2/bin/pg_ctl -D /var/pgdata/postgres16/data start'
 [root@pg_server ~]# vi /etc/rc.d/rc.local
 [root@pg_server ~]# grep -i postgres /etc/rc.d/rc.local
 su - postgres -c '/var/postgres/postgres16.2/bin/pg_ctl -D /var/pgdata/postgres16/data start'

Step 10: Update the .bash_profile of the postgres user with environment variables

 [postgres@pg_server ~]$ vi .bash_profile
 export PATH=$PATH: /var/postgres/postgres16.2/bin
 export PGDATA=/var/pgdata/postgres16/data
[root@pg_server data]# su - postgres
[postgres@pg_server ~]$ pg_ctl status
 [postgres@pg_server ~]$ psql