Skip to content
VDS · Databases

PostgreSQL: installation and your first database

Installing PostgreSQL on VDS, roles and the first database, how authentication works in pg_hba.conf, and troubleshooting connection errors.

Updated August 23, 2026

PostgreSQL is the default choice for applications in Python, Node.js, and Go. It treats data more strictly than MySQL and handles complex queries better.

Installation

apt update && apt install -y postgresql postgresql-contrib   # Debian, Ubuntu
systemctl enable --now postgresql

On AlmaLinux and CentOS Stream you need to create the cluster manually:

dnf install -y postgresql-server postgresql-contrib
postgresql-setup --initdb
systemctl enable --now postgresql

Check the version and status:

sudo -u postgres psql -c "SELECT version();"

Role and database

In PostgreSQL a user is called a role. Create a separate role for each project:

sudo -u postgres psql
CREATE ROLE shop WITH LOGIN PASSWORD 'long-random-password';
CREATE DATABASE shop OWNER shop ENCODING 'UTF8';
REVOKE ALL ON DATABASE shop FROM PUBLIC;

The connection string for the application:

postgresql://shop:password@127.0.0.1:5432/shop

Check from the system user:

psql "postgresql://shop:password@127.0.0.1:5432/shop" -c "\dt"

Authentication

Two files determine who can log in and from where.

/etc/postgresql/17/main/postgresql.conf:

listen_addresses = 'localhost'
port = 5432

/etc/postgresql/17/main/pg_hba.conf — rules are read top to bottom, and the first matching one wins:

local   all   postgres            peer
local   all   all                 scram-sha-256
host    all   all   127.0.0.1/32  scram-sha-256

The peer method means “the system user matches the role” — that is why sudo -u postgres psql works without a password, while the same command from another user does not.

After editing: systemctl reload postgresql. Never use the trust method — it lets anyone in without a password at all.

For outside access, read remote access: you must not open a port to the outside without restricting it by address.

Common errors

SymptomCause
role "..." does not existYou are connecting as a system user that has no role
password authentication failedThe rule in pg_hba.conf requires a different method, or no password is set
could not connect to server: No such file or directoryThe service is not running, or it uses a different port
permission denied for schema publicIn versions 15+ you must grant rights on public explicitly: GRANT ALL ON SCHEMA public TO shop;
FATAL: sorry, too many clientsmax_connections is exhausted — set up a pool on the application side

Logs: /var/log/postgresql/ or journalctl -u postgresql.

Next steps

Do not give the application a superuser role. A regular database owner role can do everything it needs, but it cannot drop neighbouring databases if a migration goes wrong.