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
- Create dedicated users and databases: see PostgreSQL users and databases.
- Set up a regular backup on a Backup Box.
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 pricingRelated tutorials
How to migrate a database between servers
Move a MySQL or PostgreSQL database to a new server with minimal downtime, using dump and restore.
Updated 7/20/2026DatabasesHow to secure MongoDB with authentication
By default, MongoDB runs without a password. Here is how to enable authentication and create an admin user.
Updated 7/20/2026DatabasesHow to connect an app to a remote database
Allow access to a database from another server, securely: bind address, dedicated user and firewall.
Updated 7/20/2026