Install and secure PostgreSQL
Updated 27 Nov 2026 database postgresql
Install PostgreSQL, create a database user and keep it off the internet.
Install
sudo apt update && sudo apt install -y postgresql
systemctl status postgresql
Create a user and database
sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb --owner appuser appdb
psql -h 127.0.0.1 -U appuser appdb
Keep it local
By default PostgreSQL listens only on localhost. Leave it so and use an SSH tunnel for administration.
Small-server settings
In postgresql.conf (under /etc/postgresql/), modest starting values for 2 GB of memory:
shared_buffers = 384MB
effective_cache_size = 1GB
work_mem = 8MB
max_connections = 50
Restart the service. Use a connection pool rather than raising max_connections.
On hard disks
Add indexes for frequent queries and find slow ones:
log_min_duration_statement = 500ms
Back up
sudo -u postgres pg_dump -Fc appdb > appdb-$(date +%F).dump
See database backups.