Databases Azure

PostgreSQL 18 on Ubuntu 26.04 on Azure User Guide

| Product: PostgreSQL 18 on Ubuntu 26.04 LTS on Azure

Overview

PostgreSQL is the open source object relational database known for its reliability, standards compliance and extensibility. Release 18 adds asynchronous I/O for faster sequential scans and vacuum, virtual generated columns, the uuidv7() function, OAuth authentication and skip scans on multicolumn B-tree indexes. The cloudimg image installs PostgreSQL 18.6 from the Ubuntu 26.04 LTS main archive, so Ubuntu's security updates and unattended upgrades keep the database patched with the operating system. The database files live on a dedicated Azure data disk, and each VM makes its own postgres password and TLS certificate on first boot. cloudimg's support team is on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours.

What is included:

  • PostgreSQL 18.6 server and client (postgresql-18, postgresql-client-18) from the Ubuntu 26.04 main archive
  • One cluster, 18/main, on TCP port 5432, run by systemd as postgresql@18-main and started at boot
  • The data directory on a dedicated 32 GiB Azure data disk at /var/lib/postgresql, separate from the operating system disk and independently resizable
  • No password in the image: first boot makes a unique postgres password (SCRAM-SHA-256) and a unique self-signed TLS certificate for each VM
  • Remote connections must use TLS (1.2 or later) and a password; an unencrypted remote login is refused
  • cloudimg's support team on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours

Prerequisites

An active Azure subscription, an SSH key pair, and a VNet + subnet in the target region. Standard_B2s (2 vCPU / 4 GiB RAM) is a good starting point; choose a larger size and data disk as your data grows. NSG inbound: allow 22/tcp from your management network, and open 5432/tcp only to the networks whose applications use the database. Never open 5432 to the whole internet.

Step 1 - Deploy from the Azure Marketplace

Sign in to the Azure Portal, choose Create a resource, search the Marketplace for PostgreSQL 18 on Ubuntu 26.04 by cloudimg, and select Create. On Basics pick your subscription, resource group, region and size; under Administrator account choose SSH public key and paste your key; under Inbound port rules allow SSH (22). After deployment, add an NSG rule for 5432 scoped to your own networks. Then Review + create -> Create.

Step 2 - Deploy from the Azure CLI

# Accept the Marketplace terms once per subscription (required before the first CLI deploy)
az vm image terms accept --urn cloudimg1647283583153:postgresql-ubuntu-26-04:default:latest

az vm create \
  --resource-group <your-rg> \
  --name postgresql \
  --image cloudimg1647283583153:postgresql-ubuntu-26-04:default:latest \
  --size Standard_B2s \
  --admin-username azureuser \
  --ssh-key-values ~/.ssh/id_ed25519.pub \
  --vnet-name <your-vnet> --subnet <your-subnet> \
  --public-ip-sku Standard

Step 3 - Connect to your VM

ssh azureuser@<vm-public-ip>

Step 4 - Confirm PostgreSQL is installed and running

PostgreSQL runs as the postgresql@18-main systemd unit and listens on port 5432. Check the version, the service, the cluster, the listeners and the data disk:

psql --version
systemctl is-active postgresql@18-main
pg_lsclusters
ss -tln | grep ':5432 ' | awk '{print $1, $4}'
findmnt -no SOURCE,TARGET,FSTYPE /var/lib/postgresql

You should see PostgreSQL 18.6 from the Ubuntu archive, active, the 18/main cluster online with its data directory under /var/lib/postgresql, listeners on 5432 (IPv4 and IPv6), and the dedicated data disk:

psql (PostgreSQL) 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1)
active
Ver Cluster Port Status Owner    Data directory              Log file
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log
LISTEN 0.0.0.0:5432
LISTEN [::]:5432
/dev/sdc /var/lib/postgresql ext4

PostgreSQL 18 service status, cluster, listeners and dedicated data disk

Step 5 - Retrieve your per-VM postgres password

Each VM makes its own postgres password on first boot and writes it to a root-only credentials file:

sudo cat /root/postgresql-credentials.txt
# PostgreSQL 18 on Ubuntu 26.04 LTS (cloudimg Azure Marketplace image)
# These credentials are unique to THIS VM, made on its first boot. Store them safely.

POSTGRES_USER=postgres
POSTGRES_PASSWORD=********************************
POSTGRES_HOST=74.235.238.45
POSTGRES_PORT=5432
#
# Connect from your workstation (TLS is required for remote connections):
#   psql "host=74.235.238.45 port=5432 user=postgres dbname=postgres sslmode=require"
# On the VM itself:  sudo -u postgres psql
# The server's TLS certificate is self-signed and unique to this VM.

POSTGRES_HOST is your VM's public IP address. Store the password in your secrets manager. The commands below read it from this file, so you never paste it on the command line.

Step 6 - Sign in on the VM

On the VM itself the operating system's postgres account signs in over the local socket without a password:

sudo -u postgres psql -c 'SELECT version();'
                                                             version
---------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, 64-bit
(1 row)

Step 7 - Sign in with the password over TLS

A TCP connection uses the password. Read it from the credentials file into PGPASSWORD, then confirm the login works and that the session is encrypted:

export PGPASSWORD="$(sudo grep -m1 '^POSTGRES_PASSWORD=' /root/postgresql-credentials.txt | cut -d= -f2-)"
psql "host=127.0.0.1 port=5432 user=postgres dbname=postgres sslmode=require" \
  -c 'SELECT ssl, version AS tls, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();'
unset PGPASSWORD
 ssl |   tls   |         cipher
-----+---------+------------------------
 t   | TLSv1.3 | TLS_AES_256_GCM_SHA384
(1 row)

The per-VM credentials file and a password login over TLS

Step 8 - Create an application database and role

Give each application its own role and database rather than using the postgres superuser. Choose your own strong password in place of the example:

sudo -u postgres psql -v ON_ERROR_STOP=1 <<'SQL'
CREATE ROLE appuser LOGIN PASSWORD 'change-me-Str0ng';
CREATE DATABASE appdb OWNER appuser;
SQL
CREATE ROLE
CREATE DATABASE

Sign in as the new role, create a table, write a row and read it back:

PGPASSWORD='change-me-Str0ng' psql "host=127.0.0.1 user=appuser dbname=appdb sslmode=require" -v ON_ERROR_STOP=1 <<'SQL'
CREATE TABLE notes (id serial PRIMARY KEY, body text NOT NULL, created timestamptz DEFAULT now());
INSERT INTO notes (body) VALUES ('hello from PostgreSQL 18');
SELECT id, body FROM notes;
SQL
CREATE TABLE
INSERT 0 1
 id |           body
----+--------------------------
  1 | hello from PostgreSQL 18
(1 row)

Creating a database and table, writing a row and reading it back

Step 9 - Confirm the secure defaults

The server accepts remote connections only over TLS and only with a SCRAM-SHA-256 password. Ubuntu's local rules (peer login on the socket, password on the loopback address) are kept, and two hostssl rules are added for every other address:

grep -v '^#' /etc/postgresql/18/main/pg_hba.conf | grep -v '^$'
local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
hostssl all             all             0.0.0.0/0               scram-sha-256
hostssl all             all             ::/0                    scram-sha-256

An unencrypted login from the VM's own network address is refused even with the right password, and a wrong password is refused over TLS. Each command prints the server's refusal:

export PGPASSWORD="$(sudo grep -m1 '^POSTGRES_PASSWORD=' /root/postgresql-credentials.txt | cut -d= -f2-)"
psql "host=$(hostname -I | awk '{print $1}') user=postgres dbname=postgres sslmode=disable" -c 'SELECT 1' 2>&1 \
  | grep -o 'no pg_hba.conf entry.*no encryption'
PGPASSWORD='not-the-password' psql "host=127.0.0.1 user=postgres dbname=postgres sslmode=require" -c 'SELECT 1' 2>&1 \
  | grep -o 'password authentication failed for user "postgres"'
unset PGPASSWORD
no pg_hba.conf entry for host "10.20.0.22", user "postgres", database "postgres", no encryption
password authentication failed for user "postgres"

The server settings cloudimg adds are in one file, /etc/postgresql/18/main/conf.d/10-cloudimg.conf:

cat /etc/postgresql/18/main/conf.d/10-cloudimg.conf
# cloudimg: PostgreSQL 18 on Ubuntu 26.04 LTS (Azure). Settings here override postgresql.conf.
listen_addresses = '*'
port = 5432
password_encryption = 'scram-sha-256'
ssl = on
ssl_cert_file = '/etc/postgresql-tls/server.crt'
ssl_key_file = '/etc/postgresql-tls/server.key'
ssl_min_protocol_version = 'TLSv1.2'

The remote access rules, an unencrypted login refused and a wrong password rejected

Step 10 - Connect from your own machine

Open 5432/tcp on the VM's NSG to your own address or network only, then connect with any PostgreSQL client. With psql:

psql "host=<vm-public-ip> port=5432 user=appuser dbname=appdb sslmode=require"

The server's certificate is self-signed and unique to this VM, so sslmode=require encrypts the connection without checking the certificate. To check it as well, copy /etc/postgresql-tls/server.crt from the VM to your machine and connect with sslmode=verify-ca sslrootcert=server.crt. Application drivers take the same settings, for example the connection string postgresql://appuser:<password>@<vm-public-ip>:5432/appdb?sslmode=require.

If the address in the credentials file is not the one your clients use (a VM without a public IP, a load balancer, or a DNS name), point the VM at the right one; it rewrites the credentials file and the login message, makes a new certificate for that address and restarts PostgreSQL:

sudo cloudimg-set-public-address <your-ip-or-dns-name>

Step 11 - Remove the example

When you are done with the example from Step 8, drop it:

sudo -u postgres psql -v ON_ERROR_STOP=1 -c 'DROP DATABASE appdb;' -c 'DROP ROLE appuser;'
DROP DATABASE
DROP ROLE

Maintenance

  • Persistence: the cluster's data directory, /var/lib/postgresql/18/main, is on the dedicated data disk, so your data survives reboots and the disk can be resized on its own. To grow it, resize the disk in Azure, then run sudo resize2fs "$(findmnt -no SOURCE /var/lib/postgresql)" on the VM.
  • Patching: PostgreSQL comes from the Ubuntu archive, so sudo apt update && sudo apt upgrade and the enabled unattended upgrades apply PostgreSQL security fixes with the operating system's.
  • Configuration: put your own settings (for example shared_buffers or max_connections) in a new file under /etc/postgresql/18/main/conf.d/, then sudo systemctl restart postgresql@18-main. Logs are in /var/log/postgresql/postgresql-18-main.log.
  • Password: change the postgres password with sudo -u postgres psql -c '\password postgres', then update /root/postgresql-credentials.txt or remove it.
  • Backups: pg_dump or pg_dumpall for logical backups, pg_basebackup for a physical copy, and Azure disk snapshots of the data disk for point-in-time copies of the volume.
  • TLS: for production, replace /etc/postgresql-tls/server.crt and server.key with a certificate from your own CA (key owned by root:postgres, mode 0640) and restart PostgreSQL.

Support

cloudimg's support team is on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours. Contact support@cloudimg.co.uk.

PostgreSQL is a registered trademark of the PostgreSQL Community Association of Canada. This image is provided by cloudimg and is not affiliated with or endorsed by the PostgreSQL Global Development Group.