CloudLinux.ro
Databases

How to install and secure PostgreSQL on Ubuntu

Updated 7/20/2026

PostgreSQL is prized for reliability, advanced features and standards compliance. Here is how to install it and, just as important, how to secure it on Ubuntu.

1. Install

apt-get update -y
apt-get install -y postgresql postgresql-contrib
systemctl enable --now postgresql

2. Set a password for postgres

sudo -u postgres psql
ALTER USER postgres WITH PASSWORD 'strong_password';

3. Use password authentication

In pg_hba.conf, make sure the authentication method is scram-sha-256, not trust:

local   all   all               scram-sha-256
host    all   all   127.0.0.1/32 scram-sha-256
systemctl restart postgresql

4. Keep the database local-only

In postgresql.conf, if you do not need remote access, keep:

listen_addresses = 'localhost'

Next steps

Or install it with one click

Pick PostgreSQL from the app catalog when creating the server, with an auto-generated password.

Put this guide into practice on your own VPS

EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.

See pricing

Related tutorials