Install and Configure PostgreSQL 15
Table of Contents
- Install PostgreSQL 15
- Create directories
- Change PGDATA directory
- Initialize the database
- Enable automatic start
- Enable archive mode and change archive location
- Reload PostgreSQL service
- Verify configuration
- Add firewall rules
Step 1: Install PostgreSQL 15
# Install the repository RPM: dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Disable the built-in PostgreSQL module: dnf -qy module disable postgresql # Install PostgreSQL: dnf install -y postgresql15-server
Step 2: Create directories
mkdir -p /pgTb/pgsql15/trdda
mkdir -p /pgIx/pgsql15/trdda
mkdir -p /pgBackup/pgsql15/trdda
mkdir -p /pgArch/pgsql15/arch
mkdir -p /pgData/pgsql15/data
mkdir -p /pgWal/pgsql15/wal
chown -R postgres:postgres /pg*
chmod -R 700 /pg*
Step 3: Change PGDATA directory
As root: [root@lxtrdpgdsgv01 ~]# cat /usr/lib/systemd/system/postgresql-15.service | grep -i "Environment=PGDATA" Environment=PGDATA=/pgData/pgsql15/data [root@lxtrdpgdsgv01 ~]# # systemctl daemon-reload
Step 4: Initialize the database
Run as postgres user: /usr/pgsql-15/bin/initdb -D /pgData/pgsql15/data --waldir=/pgWal/pgsql15/wal /usr/pgsql-15/bin/pg_ctl -D /pgData/pgsql15/data -l logfile start ps -ef | grep postgres /usr/pgsql-15/bin/pg_ctl -D /pgData/pgsql15/data stop
Step 5: Enable automatic start
systemctl stop postgresql-15
systemctl enable postgresql-15
systemctl start postgresql-15
systemctl status postgresql-15
Step 6: Enable archive mode and change archive location
As postgres user: cp /pgData/pgsql15/data/postgresql.conf /pgData/pgsql15/data/postgresql.conf.bkp grep -v '^#' /pgData/pgsql15/data/postgresql.conf.bkp | grep '^[A-Za-z0-9]' > /pgData/pgsql15/data/postgresql.conf edit -- /pgData/pgsql15/data/postgresql.conf # Added by DBA listen_addresses = '0.0.0.0' # ipv4 only max_wal_senders = 10 max_replication_slots = 10 wal_level = 'replica' # or 'logical' hot_standby = on archive_mode = on archive_command = 'cp %p /pgArch/pgsql17/arch/%f' # shared_preload_libraries = 'repmgr' wal_log_hints = on # End
Step 7: Reload PostgreSQL service
systemctl stop postgresql-15.service
systemctl start postgresql-15.service
Step 8: Verify configuration
sed -i "s/PGDATA=.*/PGDATA=\/pgData\/pgsql15\/data/" .bash_profile psql SHOW data_directory; SHOW archive_mode; SHOW archive_command; SHOW archive_timeout; ls -ld $(psql -t -c "SHOW data_directory")/pg_wal SELECT pg_switch_wal(); ls -lrth /pgArch/pgsql15/arch/ SELECT f AS wal_file, (pg_stat_file('/pgArch/pgsql15/arch/' || f)).size AS size_bytes, (pg_stat_file('/pgArch/pgsql15/arch/' || f)).modification AS modified_time FROM pg_ls_dir('/pgArch/pgsql15/arch') f WHERE f ~ '^[0-9A-F]{24}$' ORDER BY modified_time DESC;
Step 9: Add firewall rules
# need sudo or root user firewall-cmd --add-port=5432/tcp --permanent --zone=public firewall-cmd --reload firewall-cmd --list-ports
Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
Thank you,
DBA Learning Hub