Guest mode. You can read posts without signing up. Comments, ratings, job contacts and your own posts open after you sign up and accept the rules. What is Kommit →

Редакция КоммитРедакция портала
commit beb8a2b · 15 days ago

PostgreSQL, Part 6: Security, Minor Version Updates, and Upgrading to a New Version via pg_upgrade

🌐 Machine-translated from Russian. Show original

In this part:

Network and access#

  • Listen only to the required interfaces (listen_addresses), port 5432 is closed ufw to everyone except application servers.
  • In pg_hba.conf — specific databases, roles, and addresses instead of all all 0.0.0.0/0. For network connections — hostssl and scram-sha-256.
  • No trust in network lines. Even "temporarily."
  • External admin access is only via VPN or SSH tunnel.

Passwords: escaping with md5#

Check which roles still store passwords in the old format:

SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';

For each such role at password_encryption = 'scram-sha-256' , simply reset the password (\password имя), then replace md5 with scram-sha-256 in pg_hba.conf. In PostgreSQL 18, md5 passwords are declared expired, and warnings are displayed when used — support is planned to be removed in one of the next versions.

TLS with a normal certificate#

sudo install -o postgres -g postgres -m 600 db01.key /etc/postgresql/18/main/server.key
sudo install -o postgres -g postgres -m 644 db01.crt /etc/postgresql/18/main/server.crt

sudo tee /etc/postgresql/18/main/conf.d/70-ssl.conf >/dev/null <<'EOF'
ssl = on
ssl_cert_file = '/etc/postgresql/18/main/server.crt'
ssl_key_file = '/etc/postgresql/18/main/server.key'
ssl_min_protocol_version = 'TLSv1.2'
EOF
sudo -u postgres psql -c "SELECT pg_reload_conf();"

The certificate can be issued at an internal certification authority (for example, AD CS — see the "Active Directory and Group Policies" series). Server verification clients connect from sslmode=verify-full.

Rights: minimum principle#

WhoWhat to give
The applicationownership of its database or only the necessary rights to the scheme; without SUPERUSER, CREATEROLE
Reportspg_read_all_data or GRANT SELECT specific schemes
Monitoringpg_monitor
Backup (if not through postgres)pg_read_all_data for pg_dump, REPLICATION for physical copies of
Adminspersonal roles, not shared postgres

See who has dangerous attributes:

SELECT rolname, rolsuper, rolcreaterole, rolcreatedb, rolbypassrls
FROM pg_roles WHERE rolsuper OR rolcreaterole OR rolbypassrls;

Audit#

Basic journaling:

log_connections = on
log_disconnections = on
log_line_prefix = '%m [%p] %q%u@%d %h '
log_statement = 'ddl'          # все изменения структуры

For regulatory requirements and detailed audit (who has read which table) — the pgaudit: sudo apt install -y postgresql-18-pgauditextension, add pgaudit to shared_preload_libraries, set pgaudit.log = 'ddl, role, write'. Send logs to a separate server so the database administrator cannot silently modify them.

If the database contains personal data of Kazakhstani citizens, consider the requirements for their storage within the territory of the Republic of Kazakhstan — more details in the "Clouds in Kazakhstan" series. General protection practices — in the "Information Security and Anti-Fraud" series.

Minor updates (18.5 → 18.6)#

Only bug fixes and vulnerabilities, the data format does not change:

sudo apt update && sudo apt install --only-upgrade 'postgresql-18*' postgresql-common
sudo systemctl restart postgresql@18-main
sudo -u postgres psql -c "SHOW server_version;"

Read the release notes on the postgresql.org/docs/release before updating: sometimes it is recommended to recreate certain indexes after the update. With replicas: first update the replicas, then switch or restart the main one.

Major updates (17 → 18): ways#

MethodSimpleWhen
pg_dump → pg_restorehours on large databasessmall databases, along with "clean" data overwriting
pg_upgrade (--link, --clone)minutesmain variant
Logical replication to a new clusterseconds to switch24/7 system, with time to prepare

Update via pg_upgradecluster#

In Ubuntu, the most convenient wrapper is pg_upgradecluster: it transfers the configuration and port itself. Example: 17/main → 18/maincluster.

  1. A fresh backup and checking that it can be restored (part 3). Run the update first on the copy.
  2. Install the new version and the same extensions for it:
sudo apt install -y postgresql-18 postgresql-18-repack   # и другие нужные postgresql-18-*
  1. The package created an empty cluster 18/main — delete it, otherwise the name will be taken:
sudo pg_dropcluster 18 main --stop
  1. Update using pg_upgrade (by default pg_upgradecluster uses a slow dumpmethod, so the method is explicitly specified):
sudo pg_upgradecluster -m upgrade --link 17 main

--link creates hard links instead of copying — quickly and without double disk space, but after launching a new cluster, you cannot return to the old one. Without --link , files are copied: longer, twice as much space is needed, but the old cluster remains untouched.

  1. Check the result:
pg_lsclusters      # 18/main online на порту 5432, 17/main down
sudo -u postgres psql -c "SELECT version();"
  1. Update the scheduler statistics. PostgreSQL 18 transfers regular statistics when pg_upgrade but not extended (CREATE STATISTICS) — collect the missing ones:
sudo -u postgres vacuumdb --all --analyze-in-stages --missing-stats-only
  1. Check the application, backups (specify the new pg1-path in pgBackRest and perform stanza-upgrade), monitoring. After a few days of calm work, delete the old cluster: sudo pg_dropcluster 17 main.

Updating the replica cluster: update the primary and recreate replicas via pg_basebackup (or use the rsync procedure from pg_upgrade documentation — it's more complex).

💡 PostgreSQL 19 as of November 2026 is a release candidate status. For production, wait for the first minor releases (19.1–19.2) and the validation of your extensions and applications.

Note for 1C#

For 1C, the DBMS version is chosen for the platform, not the "latest": see which versions of the PostgreSQL build for 1C (or Postgres Pro for 1C) are supported by your platform version on releases.1c.ru and the Postgres Professional website. Postgres Pro also distributes its own backup tools — check on the provider's website. The procedure for updating the 1C server and DBMS is in the "1C Server on Windows and Linux" series.

PostgreSQL production server checklist#

  • Supported major version, latest minor release
  • Data on a separate fast volume, OS, and PostgreSQL are updated according to the schedule
  • listen_addresses, UFW, and pg_hba.conf are open only for the required addresses, hostssl + scram-sha-256
  • Personal admin roles, applications without a superuser
  • Memory, WAL, and autovacuum settings for hardware
  • Physical backups with WAL archive, off-site copy, monthly recovery test
  • Replica and switching instructions (or Patroni)
  • pg_stat_statements, postgres_exporter or Zabbix, alerts
  • Documentation: addresses, roles, where backups are, how to restore them, how to switch

🎉 Series completed. Return to content. PostgreSQL in Kubernetes with the CloudNativePG operator — in the "Kubernetes from scratch" series.

Comments 00 участников · 0 гостей🔒 Rate and discuss after signing up

Comments · 0

No comments yet.Sign in to comment, rate and see all comments.