PostgreSQL
Notes// archive From the original Code Walker docs. Kept for reference, so some versions and commands may be out of date.
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:
dnf update -yInstall PostgreSQL 12 from the module stream on RHEL8:
sudo dnf install @postgresql:12 -yOr on RHEL9, the default version is 13:
sudo dnf install postgresql-serverDatabase configuration#
First step is to initialise PostgresSQL:
sudo /usr/bin/postgresql-setup --initdbEnable and start service:
sudo systemctl enable postgresql.service --nowOpen firewall#
If firewalld is being used, add the service:
sudo firewall-cmd --add-service=postgresql --permanentReload the firewall rules:
sudo firewall-cmd --reloadAccess PostgreSQL#
Change to the postgres user:
sudo su - postgresStart the PostgreSQL interactive terminal:
psqlCreate a user and database#
Create a new user, in this case efy is an abbreviation of exampleforyou, the username is efy_user:
CREATE USER efy_user WITH PASSWORD 'changeme';The following three command demonstrate how to apply options to the user account:
ALTER ROLE efy_user SET client_encoding TO 'utf8';ALTER ROLE efy_user SET default_transaction_isolation TO 'read committed';ALTER ROLE efy_user SET timezone TO 'UTC';Create a new database:
CREATE DATABASE exampleforyou_db;Grant the user account privileges to the database:
GRANT ALL PRIVILEGES ON DATABASE exampleforyou_db TO efy_user;List and quit:
\l
\q
exitEnable external access#
Ensure it is configured to listen on the IP Address of the host.
Edit postgresql.conf:
listen_addresses = '192.168.0.70'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 identChange 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 identRestart PostgreSQL service#
systemctl restart postgresqlpgAdmin
pgAdmin is a browser based management console for PostgreSQL.
Ensure Extra Packages for Enterprise Linux (EPEL) is available:
dnf install epel-release -yAdd 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.rpmInstall pgadmin4:
dnf install pgadmin4 -yIt uses Apache, make sure mod_ssl is installed so SSL redirect works:
dnf install mod_ssl -yRun the initial setup:
/usr/pgadmin4/bin/setup-web.shEnter an email address and password to complete the installation.
The service runs on HTTPS, therefore port 443, open this service:
sudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reloadIf SELinux is set to Enforcing, add the correct context so the web server can write to the database:
dnf install policycoreutils-python-utils -ysemanage fcontext -m -t httpd_sys_rw_content_t "/var/lib/pgadmin(/.*)?"restorecon -R -v /var/lib/pgadminAccess 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:
sudo su - postgresThe following command will prompt for the password:
pg_dump -U dbuser -W changeme -h 192.168.122.50 -p 5432 my_db > my_db.sqlHowever, create a hidden file .pgpass in the home directory using the following format:
hostname:port:database:username:passwordThen the backup can be done with simply:
pg_dump my_db > my_db.sqlRestoring the Dump#
Is restoring to a new instance od PostgreSQL, you would first recreate the user and empty database, as documented earlier.
sudo su - postgrespsql -d my_db < my_db.sql