Logo ← PostgreSQL Blog

Installing Postgres from Source Code

Installing PostgreSQL from source can be an enlightening experience, especially if you want to understand the intricacies of database setup…

Installing Postgres from Source Code

Installing PostgreSQL from source can be an enlightening experience, especially if you want to understand the intricacies of database setup or need a custom installation. In this guide, we will walk through the process of installing PostgreSQL 11.4 on a RHEL9 system. We will cover everything from preparing your environment to initializing the database and setting up your environment variables.

1. Install Required Packages

Before you start, you need to install some essential packages that PostgreSQL depends on. These include development libraries and tools required for building the software.

dnf install readline-devel
dnf install -y zlib-devel
dnf install -y gcc # you must install
dnf install -y make # you must install
  • readline-devel: Provides the necessary libraries for command-line editing and history.
  • zlib-devel: Required for compression support.
  • gcc: The GNU Compiler Collection needed to compile the PostgreSQL source code.
  • make: A build tool used to compile and install the software.

2. Download and Extract Source Code

Get the PostgreSQL source code from the official repository and extract it. You can dowload other version here.

wget https://ftp.postgresql.org/pub/source/v11.4/postgresql-11.4.tar.gz

  • wget: Downloads the PostgreSQL source code.
tar -xvf postgresql-11.4.tar.gz
  • tar -xvf: Extracts the downloaded tarball.
cd postgresql-11.4/
./configure --help
  • ./configure --help: Shows available configuration options.

3. Create PostgreSQL User and Required Directories

Set up a dedicated user and directories for PostgreSQL.

useradd -d /home/postgres/ postgres

  • useradd: Creates a new user for PostgreSQL.
mkdir -p /pg_data/11.4
mkdir -p /pg_data/DATA
  • mkdir -p: Creates necessary directories for PostgreSQL data and installation.

4. Configure PostgreSQL

Configure the PostgreSQL installation and build it.

cd postgresql-11.4
./configure --prefix=/pg_data/11.4 --with-pgport=5432

  • ./configure --prefix: Sets the installation directory and port.
make
  • make: Compiles the PostgreSQL source code.
make install
  • make install: Installs PostgreSQL to the specified directory.

After installation, verify the contents:

ls -ltr /pg_data/11.4

Output
-------
total 12
drwxr-xr-x. 3 root root   24 Aug 14 10:27 share
drwxr-xr-x. 4 root root 4096 Aug 14 10:27 include
drwxr-xr-x. 4 root root 4096 Aug 14 10:27 lib
drwxr-xr-x. 2 root root 4096 Aug 14 10:27 bin

5. Configure Contributions

Compile and install PostgreSQL extensions and additional modules.

cd /postgresql-11.4/contrib

  • cd /contrib: Navigate to the contributions directory.
make
make install
  • make and make install: Build and install additional modules.

Change the ownership of the installation directories to the PostgreSQL user:

chown -R postgres:postgres /pg_data/11.4
chown -R postgres:postgres /pg_data/DATA

6. Initialize PostgreSQL Data Directory

Set up the database cluster and start PostgreSQL.

/pg_data/11.4/bin/initdb -D /pg_data/DATA/

  • initdb: Initializes the PostgreSQL data directory.
ls -lart /pg_data/DATA/

total 52
drwx------.  4 postgres postgres    30 Aug 14 10:24 ..
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_dynshmem
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_commit_ts
-rw-------.  1 postgres postgres     3 Aug 14 10:33 PG_VERSION
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_twophase
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_tblspc
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_stat_tmp
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_stat
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_snapshots
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_serial
drwx------.  2 postgres postgres     6 Aug 14 10:33 pg_replslot
drwx------.  4 postgres postgres    36 Aug 14 10:33 pg_multixact
-rw-------.  1 postgres postgres 23882 Aug 14 10:33 postgresql.conf
-rw-------.  1 postgres postgres    88 Aug 14 10:33 postgresql.auto.conf
-rw-------.  1 postgres postgres  4513 Aug 14 10:33 pg_hba.conf
-rw-------.  1 postgres postgres  1636 Aug 14 10:33 pg_ident.conf
drwx------.  3 postgres postgres    60 Aug 14 10:33 pg_wal
drwx------.  2 postgres postgres    18 Aug 14 10:33 pg_xact
drwx------.  2 postgres postgres    18 Aug 14 10:33 pg_subtrans
drwx------.  2 postgres postgres    18 Aug 14 10:33 pg_notify
drwx------.  2 postgres postgres  4096 Aug 14 10:33 global
drwx------.  5 postgres postgres    41 Aug 14 10:33 base
drwx------.  4 postgres postgres    68 Aug 14 10:33 pg_logical
drwx------. 19 postgres postgres  4096 Aug 14 10:33 .
  • pg_ctl start: Starts the PostgreSQL server
/pg_data/11.4/bin/pg_ctl -D /pg_data/DATA/status
#pg_ctl: no server running


/pg_data/11.4/bin/pg_ctl -D /pg_data/DATA/ start

/pg_data/11.4/bin/pg_ctl -D /pg_data/DATA/ status
#pg_ctl: server is running (PID: 870263)

7. Set Environment Variables

Configure your environment to include PostgreSQL binaries and libraries.

vi /var/lib/pgsql/pg.env 

Add the following lines:

export LD_LIBRARY_PATH=/pg_data/11.4/lib:$LD_LIBRARY_PATH
export PATH=/pg_data/11.4/bin:$PATH

Source the environment file:

. pg.env

8. Connect Using psql

Finally, use psql to connect to your PostgreSQL database:

psql

create database test;
CREATE DATABASE

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

This guide should help you through the PostgreSQL installation process, from preparing your system to running your database. If you encounter issues, make sure to check PostgreSQL logs and configuration files for troubleshooting. For more detailed and technical articles like this, keep following our blog on Medium. If you have any questions or need further assistance, feel free to reach out in the comments below and directly.