A. Online - Installation from Binaries
B. Offline - Installation from Source Code
Note:
1. Install the repository RPM
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2. Disable the built-in PostgreSQL module
sudo dnf -qy module disable postgresql
3. Install PostgreSQL
sudo dnf install -y postgresql16-server
4. Optionally initialize the database and enable automatic start
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
sudo systemctl enable postgresql-16
sudo systemctl start postgresql-16
5. Install extensions packages
# dnf install postgresql16-contrib.x86_64
Note:
- Building from source is only recommended for people developing PostgreSQL or extensions
- Building from source can be used for installing packages in custom location with optional features, used in upgrading for every minor version
[root@postgres-test ~]# dnf install zlib-devel readline-devel libicu-devel
2. Create directory for installation files
[root@postgres-test ~]# mkdir -p /postgreSQL/16.4
3. Download the PostgreSQL Source Code
[root@postgres-test ~]# cd /source/
[root@postgres-test source]# ls -l
...
-rw-r--r--. 1 cherry cherry 32660355 Sep 4 13:41 postgresql-16.4.tar.gz
[root@postgres-test source]# tar xvf postgresql-16.4.tar.gz
4. Configure and Install PostgreSQL
[root@postgres-test source]$ cd postgresql-16.4/
[root@postgres-test postgresql-16.4]$ ./configure --prefix=/postgreSQL/16.4 --enable-cassert
[root@postgres-test postgresql-16.4]$ make //The make command will compile the source code
[root@postgres-test postgresql-16.4]# make install // The make install will install PostgreSQL
[root@postgres-test postgresql-16.4]# cd /postgreSQL/16.4/
[root@postgres-test 16.4]# ls -l
total 12
drwxr-xr-x. 2 root root 4096 Sep 5 11:15 bin
drwxr-xr-x. 4 root root 4096 Sep 5 11:15 include
drwxr-xr-x. 4 root root 4096 Sep 5 11:15 lib
drwxr-xr-x. 3 root root 24 Sep 5 11:15 share
5. Creating Postgres User and data directory
[root@postgres-test ~]# useradd postgres
[root@postgres-test ~]# passwd postgres
[root@postgres-test ~]# mkdir -p /u01/postgreSQL/data
[root@postgres-test ~]# chown -R postgres: /u01
6. Initializing Postgres Database
[postgres@postgres-test ~]$ vi .bash_profile
...
# User specific environment and startup programs
PGDATA=/u01/postgreSQL/data
export PGDATA
export PG_HOME=/postgreSQL/16.4
export PATH=$PATH:$HOME/bin:$PG_HOME/bin
[postgres@postgres-test ~]$ initdb -D /u01/postgreSQL/data -U postgres -W
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /u01/postgreSQL/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Ho_Chi_Minh
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /u01/postgreSQL/data -l logfile start
[postgres@postgres-test ~]$ cd /u01/postgreSQL/data/
[postgres@postgres-test data]$ ls -l
total 64
drwx------. 5 postgres postgres 33 Sep 5 11:29 base
drwx------. 2 postgres postgres 4096 Sep 5 11:29 global
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_commit_ts
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_dynshmem
-rw-------. 1 postgres postgres 5711 Sep 5 11:29 pg_hba.conf
-rw-------. 1 postgres postgres 2640 Sep 5 11:29 pg_ident.conf
drwx------. 4 postgres postgres 68 Sep 5 11:29 pg_logical
drwx------. 4 postgres postgres 36 Sep 5 11:29 pg_multixact
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_notify
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_replslot
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_serial
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_snapshots
drwx------. 2 postgres postgres 6 Sep 5 12:00 pg_stat
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_stat_tmp
drwx------. 2 postgres postgres 18 Sep 5 11:29 pg_subtrans
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_tblspc
drwx------. 2 postgres postgres 6 Sep 5 11:29 pg_twophase
-rw-------. 1 postgres postgres 3 Sep 5 11:29 PG_VERSION
drwx------. 3 postgres postgres 60 Sep 5 11:29 pg_wal
drwx------. 2 postgres postgres 18 Sep 5 11:29 pg_xact
-rw-------. 1 postgres postgres 88 Sep 5 11:29 postgresql.auto.conf
-rw-------. 1 postgres postgres 29706 Sep 5 11:29 postgresql.conf
-rw-------. 1 postgres postgres 58 Sep 5 12:00 postmaster.opts
-rw-------. 1 postgres postgres 87 Sep 5 12:00 postmaster.pid
Ref:
- PostgreSQL: Linux downloads (Red Hat family)
- PostgreSQL: Documentation: 16: Chapter 17. Installation from Source Code
- How to Install PostgreSQL Using Source Code in Linux (tecmint.com)
- Installing PostgreSQL FROM Source Code (Full Installation Guide) - DEV Community
- The complete PostgreSQL 16 handbook — design, optimize, and secure | UnfoldAI