PostgreSQL
PostgreSQL is a powerful, open-source object-relational database system that has earned a strong reputation for reliability, feature robustness, and performance. In many cases, databases are external entities. Many cloud providers now provide PostgreSQL-as-a-service or project may opt for standing up a dedicated instance or instances in a highly available configuration. It may be therefore preferable to host PostgresSQL in a virtual machine locally to replicate such environments.
For best results, deploy a local Virtual Machine using Alma Linux
Following this guide for standing up a PostgreSQL database using AlmaLinux-8.5-x86_64-minimal.iso
:
Installation
Always ensure the latest updates are applied:
Install PostgreSQL 12 from the module stream on RHEL8:
Or on RHEL9, the default version is 13:
Database configuration
First step is to initialise PostgresSQL:
Enable and start service:
Open firewall
If firewalld
is being used, add the service:
Reload the firewall rules:
Access PostgreSQL
Change to the postgres
user:
Start the PostgreSQL interactive terminal:
Create a user and database
Create a new user, in this case efy
is an abbreviation of exampleforyou
, the username is efy_user
:
The following three command demonstrate how to apply options to the user account:
Create a new database:
Grant the user account privileges to the database:
List and quit:
Enable external access
Ensure it is configured to listen on the IP Address of the host.
Edit postgresql.conf:
And update the configuration to allow any host on the same subnet to access the database.
Edit pg_hba.conf
:
Original:
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
Change methods to md5 for localhost and if needed the subnet of an IP range, in this case for running PostgreSQL in a
Virtual Machine on 192.168.0.111
, adding /24
for that subnet:
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 md5
host all all 192.168.0.1/24 md5
host all all 192.168.122.1/24 md5
host all all ::1/128 md5
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
Restart PostgreSQL service
pgAdmin
pgAdmin is a browser based management console for PostgreSQL.
Ensure Extra Packages for Enterprise Linux (EPEL) is available:
Add the pgadmin
repository, note the package name is prone to change,
check https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/ for
latest:
dnf install -y https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm
Install pgadmin4
:
It uses Apache, make sure mod_ssl is installed so SSL redirect works:
Run the initial setup:
Enter an email address and password to complete the installation.
The service runs on HTTPS, therefore port 443
, open this service:
If SELinux is set to Enforcing, add the correct context so the web server can write to the database:
Access pgAdmin using a browser, for example: https://localhost/pgadmin4
Then simply add a new server, using localhost and the credentials added previously.
Backup & Restore
SQL Dump
On the PostgreSQL server, change to the postgres
user:
The following command will prompt for the password:
However, create a hidden file .pgpass
in the home directory using the following format:
Then the backup can be done with simply:
Restoring the Dump
Is restoring to a new instance od PostgreSQL, you would first recreate the user and empty database, as documented earlier.