PostgreSQL 14 and pgAdmin 4 on AlmaLinux 8 on Azure User Guide
Overview
This image runs PostgreSQL 14 and the pgAdmin 4 web console on AlmaLinux 8, ready from the first boot. PostgreSQL 14 comes from the official PGDG repository and listens on 127.0.0.1:5432 only. pgAdmin 4 (server mode) is served by Apache httpd on port 80 at /pgadmin4.
Every virtual machine gets its own passwords. On the first boot the service cloudimg-firstboot-postgresql-pgadmin sets a random password for the postgres database role and creates the pgAdmin 4 login admin@example.com with another random password, writes both to /root/postgresql-pgadmin-credentials.txt (readable by root only) and never runs again. Two machines from this image never share a password.
What is included:
- PostgreSQL 14 server and client (
/usr/pgsql-14), data directory/var/lib/pgsql/14/data - pgAdmin 4 (web, server mode) under Apache httpd, at
http://<vm>/pgadmin4 - Per-VM passwords created on first boot
- AlmaLinux 8.10 with the Azure Linux Agent and cloud-init
Platform: Microsoft Azure, Gen1 and Gen2 plans
Login user: the administrator username you choose at deploy time (azureuser in the examples)
Prerequisites
- An Azure subscription that can create virtual machines
- An SSH key pair
- The Azure CLI if you deploy from the command line
Recommended VM size: Standard_B2ms (2 vCPU, 8 GB RAM). A larger size suits a busier database.
Step 1: Deploy the virtual machine
Option A: Azure Portal
- In the Azure Marketplace, search for PostgreSQL 14 pgAdmin4 Alma Linux 8 cloudimg and select Create.
- On Basics, choose your subscription, resource group, region and a VM name. Pick the Gen2 plan unless you need Gen1. Set Size to
Standard_B2ms. - Under Administrator account, choose SSH public key and give your username and key.
- Under Inbound port rules, allow SSH (22) only, and restrict it to your own address. pgAdmin 4 is reached through an SSH tunnel (Step 4), so port 80 does not need to be open.
- Select Review + create, then Create.
Option B: Azure CLI
Accept the Marketplace terms once per subscription, then create the VM. The Gen2 plan:
az vm image terms accept --urn cloudimg1647283583153:32ed21e9-51b1-493c-9e50-68819dafe09a:32ed21e9-51b1-493c-9e50-68819dafe09a-gen2:latest
az group create --name myResourceGroup --location eastus
az vm create \
--resource-group myResourceGroup \
--name my-postgresql14 \
--image cloudimg1647283583153:32ed21e9-51b1-493c-9e50-68819dafe09a:32ed21e9-51b1-493c-9e50-68819dafe09a-gen2:latest \
--size Standard_B2ms \
--admin-username azureuser \
--generate-ssh-keys \
--public-ip-sku Standard
For the Gen1 plan, use the URN cloudimg1647283583153:32ed21e9-51b1-493c-9e50-68819dafe09a:32ed21e9-51b1-493c-9e50-68819dafe09a:latest.
az vm create opens port 22 to any address by default. Narrow it to your own address in the VM's network security group.
Step 2: Connect over SSH
Find the public IP address:
az vm show --resource-group myResourceGroup --name my-postgresql14 --show-details --query publicIps --output tsv
Then connect:
ssh azureuser@<public-ip>
Step 3: Read your credentials
The first-boot service has finished by the time you can log in. Read the file it wrote:
sudo cat /root/postgresql-pgadmin-credentials.txt

It names the PostgreSQL superuser postgres and its password, and the pgAdmin 4 login admin@example.com and its password. Keep both somewhere safe.
Check that both services are running:
systemctl status postgresql-14 httpd --no-pager

Connect with psql as postgres. Password logins work over 127.0.0.1; paste the password from the credentials file when asked:
/usr/pgsql-14/bin/psql --version
/usr/pgsql-14/bin/psql -h 127.0.0.1 -U postgres -c 'SELECT version();'

On the machine itself you can also work as the postgres operating system user, with no password:
sudo -u postgres /usr/pgsql-14/bin/psql
Step 4: Open pgAdmin 4 through an SSH tunnel
pgAdmin 4 serves plain HTTP, so reach it through SSH rather than opening port 80. On your own computer:
ssh -N -L 8080:127.0.0.1:80 azureuser@<public-ip>
Leave that running and browse to http://127.0.0.1:8080/pgadmin4/. The first page load after a boot can take a few seconds while pgAdmin starts.

Sign in with admin@example.com and the pgAdmin password from the credentials file.

Step 5: Register the local PostgreSQL 14 server
No server is registered in pgAdmin yet. Select Add New Server on the dashboard, and:
- On General, give it a name, for example
PostgreSQL 14 (localhost). - On Connection, set Host name/address to
127.0.0.1and Port to5432. - Set Username to
postgres. pgAdmin fills inadmin, taken from your login email, and that connection fails. - Enter the PostgreSQL password from the credentials file, turn on Save password? if you want, and select Save.

The server appears under Servers and the dashboard shows its live activity.

Step 6: Create a database and an application role
Connect your applications with their own role, not postgres:
sudo -u postgres /usr/pgsql-14/bin/psql -c "CREATE DATABASE myapp;"
sudo -u postgres /usr/pgsql-14/bin/psql -c "CREATE ROLE appuser WITH LOGIN PASSWORD 'choose-a-strong-password';"
sudo -u postgres /usr/pgsql-14/bin/psql -c "GRANT ALL PRIVILEGES ON DATABASE myapp TO appuser;"
sudo -u postgres /usr/pgsql-14/bin/psql -c "\l"
In pgAdmin the same is Databases → Create → Database and Login/Group Roles → Create → Login/Group Role.
Step 7: Allow connections from your virtual network
PostgreSQL listens on 127.0.0.1 only. To let other machines in your virtual network connect:
- In
/var/lib/pgsql/14/data/postgresql.conf, setlisten_addresses = '*'. - In
/var/lib/pgsql/14/data/pg_hba.conf, add a line for your subnet, for examplehost all all 10.0.0.0/24 md5. - Restart PostgreSQL:
sudo systemctl restart postgresql-14. - Allow TCP 5432 from that subnet only in the network security group. firewalld is not running on this image.
Never open 5432 to the internet.
Step 8: Back up and restore
sudo -u postgres /usr/pgsql-14/bin/pg_dump -Fc myapp > /tmp/myapp.dump
sudo -u postgres /usr/pgsql-14/bin/pg_restore -d myapp --clean /tmp/myapp.dump
sudo -u postgres /usr/pgsql-14/bin/pg_dumpall > /tmp/all-databases.sql
Copy backups off the machine, for example to Azure Blob Storage with az storage blob upload.
Server components
| Component | Version | Where |
|---|---|---|
| PostgreSQL | 14 (PGDG packages) | /usr/pgsql-14, service postgresql-14 |
| pgAdmin 4 | 9 (web, server mode) | /usr/pgadmin4, served by httpd at /pgadmin4 |
| Apache httpd | 2.4 with mod_wsgi | service httpd, port 80 |
| AlmaLinux | 8.10 |
Filesystem layout
| Path | What it holds |
|---|---|
/ |
The OS disk (about 64 GB), one partition |
/boot, /boot/efi |
Boot partitions |
/mnt |
The Azure temporary disk; its contents are lost on deallocation |
/var/lib/pgsql/14/data |
PostgreSQL data, configuration and logs (log/) |
/var/lib/pgadmin |
pgAdmin 4 configuration database and sessions |
/var/log/pgadmin, /var/log/httpd |
pgAdmin 4 and Apache logs |
/root/postgresql-pgadmin-credentials.txt |
The per-VM passwords |
Managing the services
sudo systemctl status postgresql-14 httpd
sudo systemctl restart postgresql-14
sudo systemctl restart httpd
Scripts and log files
| File | Purpose |
|---|---|
/usr/local/sbin/cloudimg-firstboot-postgresql-pgadmin |
Sets the per-VM passwords on first boot |
/var/lib/cloudimg/postgresql-pgadmin-initialised |
Marks that the first boot has run |
/var/lib/pgsql/14/data/log/postgresql-<day>.log |
PostgreSQL log, one file per weekday |
/var/log/pgadmin/pgadmin4.log |
pgAdmin 4 log |
On startup
The unit cloudimg-firstboot-postgresql-pgadmin.service runs once, after PostgreSQL and httpd start, and only when /var/lib/cloudimg/postgresql-pgadmin-initialised is absent. After that every boot simply starts postgresql-14 and httpd. No update script runs at boot. Apply operating system updates yourself:
sudo dnf upgrade -y
Troubleshooting
- pgAdmin answers 404 or is slow straight after a boot: pgAdmin is starting. Reload after a few seconds.
- "password authentication failed for user admin" when registering the server: set Username to
postgreson the Connection tab. - psql asks for a password as
postgresand rejects it: use-h 127.0.0.1, or runsudo -u postgres psqlon the machine. - The credentials file is missing: check
systemctl status cloudimg-firstboot-postgresql-pgadminandjournalctl -u cloudimg-firstboot-postgresql-pgadmin.
Security recommendations
- Keep port 22 limited to your own address and reach pgAdmin through the SSH tunnel. If you must publish pgAdmin, put it behind HTTPS (
mod_ssland a certificate) and restrict port 443 to known addresses. - Change the
postgrespassword once you have stored it elsewhere (ALTER ROLE postgres WITH PASSWORD '...';), then delete the credentials file. - Change the pgAdmin password from the user menu (Change Password).
- Give each application its own role with only the rights it needs.
- Apply updates regularly with
sudo dnf upgrade.
Support
cloudimg's support team is on hand 24 hours a day by email at support@cloudimg.co.uk, with a guaranteed response within 24 hours. For PostgreSQL see https://www.postgresql.org/docs/14/ and for pgAdmin 4 see https://www.pgadmin.org/docs/.