
PostgreSQL, part 1: installation from the official repository on Ubuntu 24.04, network access, pg_hba, and roles
🌐 Machine-translated from Russian. Show original
In this part:
- In simple terms
- Which version to install
- Step 1. Connect the PGDG repository
- Step 2. Installation
- Step 3. Localization and encoding
- Step 4. Allow network connections
- Step 5. pg_hba.conf — who can log in and from where
- Step 6. Firewall
- Step 7. Roles and bases
- Step 8. Verification from the application server
- psql cheat sheet
- Note for 1C
In simple terms#
- PostgreSQL is a free open-source DBMS. It runs websites, CRM, 1C, portals, and analytics.
- In PostgreSQL terms**, a cluster** is a single server instance with a data directory and port (usually 5432). It can contain many databases.
- Role — a user or group. A role with
LOGINrights is an account to connect. - WAL (pre-write log) is a log of all changes. It contains recovery from failures, point-in-time backups, and replication.
Which version to install#
| Version | Status as of November 2026 | Support up to |
|---|---|---|
| ** | 18** is relevant, we set it | November 2030 |
| 17 supported November | , 2029 | |
| 16 | supported November | , 2028 |
| 15 supported November | , 2027 | |
| 14 | last days of support | November 12, 2026 |
Each major version receives fixes for 5 years. Minor releases (18.5 → 18.6) are released about once a quarter and contain only fixes — always install them. The current table is postgresql.org/support/versioning.
💡 Ubuntu 24.04 has PostgreSQL 16 "out of the box." It is working, but the official PGDG repository (apt.postgresql.org) provides fresh versions, all minor updates on release day, and extension packages: pg_repack, pgaudit, PostGIS, and others.
Step 1. Connect the PGDG repository#
The simplest way is a script from the postgresql-commonpackage:
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
If you prefer to do everything by hand — just like on the postgresql.org/download/linux/ubuntu:
sudo apt install -y curl ca-certificates gpg
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail \
https://www.postgresql.org/media/keys/ACCC4CF8.asc
gpg --show-key /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc
# отпечаток должен быть B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
sudo tee /etc/apt/sources.list.d/pgdg.sources >/dev/null <<'EOF'
Types: deb
URIs: https://apt.postgresql.org/pub/repos/apt
Suites: noble-pgdg
Architectures: amd64
Components: main
Signed-By: /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc
EOF
noble is the codename of Ubuntu 24.04. For another version, see yours: lsb_release -cs.
Step 2. Installation#
sudo apt update
sudo apt install -y postgresql-18
The package immediately creates and launches the 18/maincluster. Where everything lies (that's how it works in Debian and Ubuntu):
| What | Where |
|---|---|
| Configuration | /etc/postgresql/18/main/postgresql.conf, pg_hba.conf |
| Customize your settings as separate files | /etc/postgresql/18/main/conf.d/*.conf |
| Data | /var/lib/postgresql/18/main |
| Journal | /var/log/postgresql/postgresql-18-main.log |
| Service | postgresql@18-main |
Useful Debian wrapper commands:
pg_lsclusters # список кластеров, порты, статус
sudo systemctl status postgresql@18-main
sudo -u postgres psql -c "SELECT version();"
⚠️ Put data on a fast SSD/NVMe. A good practice is to have a separate volume mounted in the
/var/lib/postgresqlbefore installing the package.
Step 3. Localization and encoding#
The cluster is created with the system locale. Make sure it is the one you need:
locale
sudo -u postgres psql -c "SELECT datname, encoding, datcollate FROM pg_database;"
For Russian data, ru_RU.UTF-8 or C.UTF-8 with UTF8 encoding is usually needed. If the cluster is still empty, it is easier to recreate it with the desired locale:
sudo locale-gen ru_RU.UTF-8
sudo pg_dropcluster 18 main --stop
sudo pg_createcluster 18 main --locale=ru_RU.UTF-8 --start
Step 4. Allow network connections#
By default, PostgreSQL only listens to localhost. It's convenient to keep your settings in a separate file — so you won't have to compare a large postgresql.confwhen updating the package:
sudo tee /etc/postgresql/18/main/conf.d/10-network.conf >/dev/null <<'EOF'
listen_addresses = 'localhost,192.168.1.21' # адрес сервера в локальной сети
port = 5432
max_connections = 200
password_encryption = 'scram-sha-256'
EOF
Don't set listen_addresses = '*' without a reason: listen only to the right interfaces.
Step 5. pg_hba.conf — who can log in and from where#
The file is read from top to bottom, and the first appropriate line is triggered. Format: connection type, base, role, address, method.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 scram-sha-256
hostssl appdb app 192.168.1.30/32 scram-sha-256
hostssl all dba 192.168.1.0/24 scram-sha-256
| Method | What does | mean
| --- | --- |
| peer | passwordless login if the Linux username matches the role (locally only) |
| scram-sha-256 | password, a modern and secure way to |
| md5 | deprecated password storage method; deprecated in PostgreSQL 18 — do not use in new installations |
| trust | passwordless — never for network connections |
| reject | explicit prohibition |
You can apply changes pg_hba.conf without restarting, and changing listen_addresses requires a restart:
sudo systemctl restart postgresql@18-main
sudo -u postgres psql -c "SELECT * FROM pg_hba_file_rules WHERE error IS NOT NULL;" # ошибки в pg_hba
For hostssl , SSL is enabled. In Ubuntu, a cluster is created with ssl = on and a self-signed certificate — for an internal network, this is sufficient; we will analyze the correct certificate in part 6.
Step 6. Firewall#
sudo ufw allow from 192.168.1.30 to any port 5432 proto tcp # сервер приложения
sudo ufw allow from 192.168.1.0/24 to any port 5432 proto tcp # сеть админов — при необходимости
⚠️ Never publish port 5432 online. Remote access — via VPN or SSH tunnel:
ssh -L 5432:127.0.0.1:5432 ivan@db01.
Step 7. Roles and bases#
Rule: each application has its own role and its own database. Superuser postgres — only for administration.
sudo -u postgres psql
-- личная роль администратора
CREATE ROLE dba LOGIN CREATEDB CREATEROLE;
\password dba
-- роль приложения и его база
CREATE ROLE app LOGIN;
\password app
CREATE DATABASE appdb OWNER app;
-- роль «только чтение» для отчетов
CREATE ROLE report LOGIN;
\password report
GRANT pg_read_all_data TO report; -- встроенная роль: чтение всех таблиц
The \password command interactively asks for the password and does not leave it in the team history or logs — unlike ALTER ROLE ... PASSWORD '...'.
Starting with PostgreSQL 15, regular users cannot create objects in a schema public someone else's database — this is safer than before. The database owner (app) still can.
Step 8. Verification from the application server#
sudo apt install -y postgresql-client
psql "host=192.168.1.21 dbname=appdb user=app sslmode=require" -c "SELECT current_user, inet_server_addr();"
psql cheat sheet#
| Command | What does |
|---|---|
\l | list of |
\c appdb | connect to the database |
\dt, table \d имя | , table structure, |
\du | roles |
\x | vertical output of wide lines |
\timing | request execution time |
\q | exit |
Note for 1C#
For 1C databases, a special PostgreSQL build with platform modifications is required: from 1C (section "PostgreSQL Distributions" on releases.1c.ru) or Postgres Pro for 1C from Postgres Professional. The PGDG packages from this article are not suitable for 1C and should not be mixed with the 1C build on a single server. Installing the build for 1C is in the "1C Server on Windows and Linux" series. Everything else in this series — roles, pg_hba, backups, monitoring, maintenance — also applies to it.
Next is PostgreSQL, part 2: tuning memory, disk, autovacuum, and connections, the PgBouncer pool.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.