1. Install PostgreSQL
On Ubuntu 24.04, after securing the server, the packaged version is PostgreSQL 16:
sudo apt install -y postgresql sudo -u postgres psql -c "SELECT version();"
For the newest release, add the PostgreSQL project's own repository first and install that version's package:
sudo apt install -y postgresql-common sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh apt-cache search --names-only '^postgresql-[0-9]+$' # versions on offer
Before loading data, check how the server's drives are laid out, as in checking a new dedicated server, and put the data directory on the mirror or array you want it on.
2. Starting Settings for Each Server
Worked out from each of our servers' memory and cores with the rules above, assuming the server runs only PostgreSQL, up to 200 connections and 4 parallel workers per query. They are starting points: measure with your own queries and adjust.
| Server | Memory | shared_buffers | effective_cache_size | maintenance_work_mem | work_mem | From |
|---|---|---|---|---|---|---|
| AMD Ryzen 9 5950X | 128 GB | 32GB | 96GB | 2GB | 40MB | $399.00/mo |
| AMD EPYC 4585PX | 128 GB | 32GB | 96GB | 2GB | 40MB | $849.00/mo |
| AMD Ryzen 9 9950X | 192 GB | 48GB | 144GB | 2GB | 61MB | $669.00/mo |
| AMD EPYC 7443P | 512 GB | 128GB | 384GB | 2GB | 163MB | $969.00/mo |
| AMD EPYC 9455P | 512 GB | 128GB | 384GB | 2GB | 163MB | $999.00/mo |
| AMD Dual EPYC 7702 | 512 GB | 128GB | 384GB | 2GB | 163MB | $1,099.00/mo |
| AMD EPYC 9455 | 512 GB | 128GB | 384GB | 2GB | 163MB | $2,299.00/mo |
3. Apply the Settings
Ubuntu's packages read every file in the version's conf.d folder, so keep your changes in one file of their own. For a server with 128 GB and 16 cores:
# /etc/postgresql/16/main/conf.d/tuning.conf (use your version's number) shared_buffers = 32GB effective_cache_size = 96GB maintenance_work_mem = 2GB work_mem = 40MB max_connections = 200 # NVMe: random reads cost little more than sequential ones random_page_cost = 1.1 effective_io_concurrency = 200 # Use the cores max_worker_processes = 16 max_parallel_workers = 16 max_parallel_workers_per_gather = 4 max_parallel_maintenance_workers = 4 # Fewer, smoother checkpoints under heavy writes max_wal_size = 8GB min_wal_size = 2GB
sudo systemctl restart postgresql sudo -u postgres psql -c "SHOW shared_buffers;"
4. Huge Pages for Large shared_buffers
With tens of gigabytes of shared_buffers, huge pages cut the memory the kernel spends tracking it. PostgreSQL 15 and later reports exactly how many it needs:
sudo -u postgres /usr/lib/postgresql/16/bin/postgres \ -D /var/lib/postgresql/16/main -c config_file=/etc/postgresql/16/main/postgresql.conf \ -C shared_memory_size_in_huge_pages # reserve that many (example: 16500), then restart PostgreSQL echo "vm.nr_hugepages = 16500" | sudo tee /etc/sysctl.d/60-hugepages.conf sudo sysctl --system sudo systemctl restart postgresql
Run the first command with PostgreSQL stopped. The default huge_pages = try uses them once they are reserved.
5. Keep It Private
PostgreSQL listens only on the server itself by default. If an application on another server needs it, set listen_addresses to the address it should listen on, allow only that application's address in pg_hba.conf with scram-sha-256, and open port 5432 only to it:
# pg_hba.conf host appdb appuser 198.51.100.21/32 scram-sha-256 sudo ufw allow from 198.51.100.21 to any port 5432 proto tcp
Never open 5432 to everyone: database ports are scanned constantly. To reach it from your own computer, use an SSH tunnel: ssh -L 5432:127.0.0.1:5432 [email protected].
6. Back It Up Off the Server
A mirror protects against a failed drive, not against a dropped table or a lost server. Dump each database every night to storage somewhere else, as in backing up a database to S3, and test a restore now and then. For large databases or point-in-time recovery, use a tool built for it, such as pgBackRest, writing to object storage.