Databases Azure

PostgreSQL 14 and pgAdmin 4 on AlmaLinux 8 on Azure User Guide

| Product: PostgreSQL 14 & pgAdmin4 on Alma Linux 8

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

  1. In the Azure Marketplace, search for PostgreSQL 14 pgAdmin4 Alma Linux 8 cloudimg and select Create.
  2. 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.
  3. Under Administrator account, choose SSH public key and give your username and key.
  4. 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.
  5. 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

The per-VM credentials file, passwords masked

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

postgresql-14 and httpd both active (running)

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();'

psql reports PostgreSQL 14

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.

The pgAdmin 4 login page

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

The pgAdmin 4 dashboard after the first login

Step 5: Register the local PostgreSQL 14 server

No server is registered in pgAdmin yet. Select Add New Server on the dashboard, and:

  1. On General, give it a name, for example PostgreSQL 14 (localhost).
  2. On Connection, set Host name/address to 127.0.0.1 and Port to 5432.
  3. Set Username to postgres. pgAdmin fills in admin, taken from your login email, and that connection fails.
  4. Enter the PostgreSQL password from the credentials file, turn on Save password? if you want, and select Save.

Registering the local server: host 127.0.0.1, username postgres

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

pgAdmin 4 connected to PostgreSQL 14

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:

  1. In /var/lib/pgsql/14/data/postgresql.conf, set listen_addresses = '*'.
  2. In /var/lib/pgsql/14/data/pg_hba.conf, add a line for your subnet, for example host all all 10.0.0.0/24 md5.
  3. Restart PostgreSQL: sudo systemctl restart postgresql-14.
  4. 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 postgres on the Connection tab.
  • psql asks for a password as postgres and rejects it: use -h 127.0.0.1, or run sudo -u postgres psql on the machine.
  • The credentials file is missing: check systemctl status cloudimg-firstboot-postgresql-pgadmin and journalctl -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_ssl and a certificate) and restrict port 443 to known addresses.
  • Change the postgres password 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/.