Guideschevron_rightServers

Servers · 10 min read · Updated

How to Install and Tune PostgreSQL on a Dedicated Server

To run PostgreSQL well on a dedicated server, install it from the Ubuntu or PostgreSQL repository, then size its memory to the machine instead of keeping the defaults, which are made for a tiny server. On a server that runs only the database, start with shared_buffers at 25% of memory, effective_cache_size at 75%, maintenance_work_mem up to 2 GB and a modest work_mem; tell the planner the disks are NVMe; keep it off the public internet; and back it up off the server every day.

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.

Starting PostgreSQL memory settings for each dedicated server
ServerMemoryshared_bufferseffective_cache_sizemaintenance_work_memwork_memFrom
AMD Ryzen 9 5950X128 GB32GB96GB2GB40MB$399.00/mo
AMD EPYC 4585PX128 GB32GB96GB2GB40MB$849.00/mo
AMD Ryzen 9 9950X192 GB48GB144GB2GB61MB$669.00/mo
AMD EPYC 7443P512 GB128GB384GB2GB163MB$969.00/mo
AMD EPYC 9455P512 GB128GB384GB2GB163MB$999.00/mo
AMD Dual EPYC 7702512 GB128GB384GB2GB163MB$1,099.00/mo
AMD EPYC 9455512 GB128GB384GB2GB163MB$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.

Common Questions

  • How much shared_buffers should PostgreSQL have?

    A quarter of the server's memory is the usual starting point on a server that runs only the database. PostgreSQL also relies on the operating system's file cache, which is why effective_cache_size is set to about three quarters: it tells the planner how much data is likely to be cached between the two.

  • Why not give work_mem a large value?

    work_mem applies to each sort or hash step of each query, not to the server as a whole. One complex query can use it several times, and every connection can run such queries at once, so a large value multiplied by many connections can run the server out of memory.

  • Dedicated server or VPS for a database?

    A VPS suits a small database. Once the data you use often outgrows its memory, or queries are slowed by other work on the host, a dedicated server keeps the working set in memory, gives the database every core and puts its files on local NVMe.

  • Do I need a connection pooler?

    Beyond a couple of hundred connections, yes. Each connection is its own process with its own memory. PgBouncer in front of PostgreSQL lets thousands of clients share a small pool of real connections.

Plans and prices

Dedicated Servers

Bare-metal servers with NVMe drives and up to hundreds of gigabytes of memory, all of it for your database.

See all plans
  • AMD Ryzen 9 5950X

    16 Cores · 128 GB RAM · 2x 1TB NVMe

    Ashburn, VA · Dallas, TX · 50 TB transfer · 10 GigE

    $399.00/mo

  • AMD Ryzen 9 9950X

    16 Cores · 192 GB RAM · 2x 2 TB NVMe

    Ashburn, VA · Dallas, TX · 50 TB transfer · 10 GigE

    $669.00/mo

  • AMD EPYC 4585PX

    16 Cores · 128 GB RAM · 2x 6.4TB NVMe Gen4

    Dallas, TX · 200 TB transfer · 50 Gbps

    $849.00/mo

  • AMD EPYC 7443P

    24 Cores · 512 GB RAM · 2x 2TB NVMe

    Dallas, TX · Ashburn, VA · 50 TB transfer · 10 GigE

    $969.00/mo

Keep Reading

All guides