PostgreSQL 17 on a VPS: Installation and Performance Tuning Basics

PostgreSQL 17 on a VPS is a solid pick when your app outgrows SQLite or needs proper JSON, full-text search, and concurrent writes. This guide installs it from the official PostgreSQL repository on Ubuntu 24.04 and tunes the settings that matter on a small server, so you end up with a database that stays up instead of one that eats all your RAM on the first busy day.

Ubuntu 24.04 ships an older PostgreSQL in its default repositories, so the official PGDG repository is the way to get version 17 and keep it updated. The steps below take about ten minutes on a fresh VPS. If you are still deciding between managed and self-hosted, this comparison of Aiven versus self-hosted PostgreSQL pricing shows what you pay for when someone else runs the database for you.

PostgreSQL 17 on a VPS: Install from the Official Repository

Start by adding the PGDG repository and its signing key, then install the server package:

sudo apt update
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install -y postgresql-17

The install script asks which version to enable. Pick 17, and the package pulls in the server, the client tools, and a default cluster named main. Check that the service is running:

sudo systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"

The second command prints the server version. If it shows PostgreSQL 17, the install worked. Next, set a password for the postgres superuser and create a database and user for your app:

sudo -u postgres psql
password postgres
CREATE USER appuser WITH PASSWORD 'use-a-long-random-password';
CREATE DATABASE appdb OWNER appuser;
q

Use a password manager to generate the password. A short or reused password on a database that listens on the network is how small servers get owned. By default PostgreSQL only listens on localhost, and you should keep it that way unless you have a specific reason and a firewall rule to match.

First Checks After Installation

Before tuning anything, confirm how much memory the VPS actually has and what else runs on it. A 2 GB VPS that also runs Nginx and PHP-FPM leaves far less for the database than a dedicated database box. Run free -h and note the available memory at idle. Then look at the default configuration:

sudo -u postgres psql -c "SHOW shared_buffers; SHOW work_mem; SHOW max_connections;"

The defaults come from a template written for a much bigger machine. On Ubuntu, shared_buffers defaults to 128 MB, work_mem to 4 MB, and max_connections to 100. Those defaults are safe but slow for a real workload, and max_connections at 100 can hurt you: every connection reserves memory, so a hundred idle connections on a 2 GB box waste RAM that the buffer cache could use.

Find your configuration file first, because the path differs by install method. For the PGDG package on Ubuntu 24.04 it lives at /etc/postgresql/17/main/postgresql.conf. Always back it up before editing:

sudo cp /etc/postgresql/17/main/postgresql.conf /etc/postgresql/17/main/postgresql.conf.bak

shared_buffers and the Rest of the Memory Settings

The one setting that decides most of your read performance is shared_buffers. PostgreSQL keeps table data and indexes in this cache, so a bigger buffer means fewer disk reads. The long-standing guideline is about 25 percent of total RAM, and it holds up well on small servers:

shared_buffers = 512MB
work_mem = 8MB
maintenance_work_mem = 128MB
effective_cache_size = 1536MB

These values fit a 2 GB VPS that also runs a web server. shared_buffers at 512 MB leaves room for the operating system, Nginx, and PHP-FPM. work_mem is per sort and hash operation per connection, so keep it small: 8 MB times twenty concurrent sorts is already 160 MB. maintenance_work_mem only matters during VACUUM and index builds, so 128 MB is safe. effective_cache_size tells the planner how much memory the OS page cache plus shared buffers can hold, and 1536 MB is a fair estimate on a 2 GB box.

Do not copy settings from a tuning guide written for a 64 GB server. A shared_buffers of 4 GB on a 2 GB VPS does not make reads faster; it makes the kernel swap, and a swapped database is slower than a small cache. If your VPS also runs MySQL for another app, the same rule applies there, and this guide to MySQL 8.4 tuning on a 2 GB VPS shows how the two databases split the same small memory budget.

Connection Settings That Keep a Small VPS Alive

Most small apps need far fewer connections than the default 100. A WordPress site or a small API usually runs fine with 30 to 50. Lower the limit and the memory math gets easier:

max_connections = 50

If your app opens a new connection per request and you see connection errors under load, the fix is a connection pooler, not a bigger max_connections. PgBouncer in transaction mode lets hundreds of app connections share a small pool of real database connections, and it uses almost no memory itself. That is the right answer for a PHP app behind PHP-FPM, where every worker process wants its own connection.

Also set a sane idle_in_transaction_session_timeout so a forgotten transaction cannot hold locks and bloat tables forever. Ten minutes is a reasonable starting point:

idle_in_transaction_session_timeout = 10min

Restart PostgreSQL after changing these settings, then check the log for errors before you call the job done:

sudo systemctl restart postgresql
sudo tail -50 /var/log/postgresql/postgresql-17-main.log

Autovacuum and WAL Settings Worth Touching

PostgreSQL cleans up dead rows with autovacuum, and the defaults are conservative. On a small VPS with a write-heavy app, dead rows pile up faster than the default autovacuum notices, so tables bloat and queries slow down. These two changes make autovacuum react sooner:

autovacuum_vacuum_scale_factor = 0.05
autovacuum_analyze_scale_factor = 0.02

The defaults are 0.2 and 0.1, which means autovacuum waits until a fifth of the table is dead rows. At 0.05 it starts after five percent. On a small database the extra vacuum runs cost almost nothing and keep tables lean.

For write-ahead log settings, the default wal_buffers of 16 MB is fine for a small server, and checkpoint_timeout at 15 minutes is reasonable. One change worth making is min_wal_size and max_wal_size, which control how much WAL PostgreSQL keeps before forcing a checkpoint. On a 2 GB VPS, max_wal_size = 1GB keeps checkpoints from stalling the disk too often. Leave the rest alone until you have a real problem to solve.

Check Performance with pg_stat_statements

Guessing which query is slow wastes time. PostgreSQL 17 ships pg_stat_statements as a contrib module, and enabling it gives you a ranked list of queries by total time:

shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all

Add those two lines to postgresql.conf, restart, then create the extension in your database:

sudo -u postgres psql -d appdb -c "CREATE EXTENSION pg_stat_statements;"

After a day of real traffic, ask for the slowest queries:

SELECT query, calls, mean_exec_time, total_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC LIMIT 10;

Fix the top query first. It is usually a missing index or a query that scans a whole table for a handful of rows. Add the index, rerun the query, and watch the number drop. That loop beats every generic tuning checklist, because it targets your actual workload instead of a stranger’s.

While you are in there, check the cache hit ratio. A ratio above 99 percent means almost every read hits memory, which is what you want on a small VPS:

SELECT sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) AS cache_hit_ratio
FROM pg_statio_user_tables;

If the ratio sits well below that, the buffer cache is too small or a query is scanning tables it should not touch. Both problems show up in pg_stat_statements first, so start there.

What to Skip on a Small VPS

Not every PostgreSQL feature earns its memory on a 2 GB server. Skip parallel query tuning until you have a slow query that a bigger work_mem and an index cannot fix; the defaults are fine for a small app. Skip logical replication unless you genuinely run a second server. Skip huge effective_io_concurrency values, which only help on fast NVMe arrays with many parallel reads.

Backups matter more than any of these settings. A tuned database with no backup is one disk failure away from nothing, so set up a nightly pg_dump before you tune anything else. This guide to backing up PostgreSQL with pg_dump and point-in-time recovery covers the setup, and it takes less time than the tuning above.

Finally, size the VPS to the database, not the other way around. If the database grows past a few gigabytes and the cache hit ratio keeps falling, the honest fix is more RAM, not more settings. This comparison of Linode versus Hetzner for database-heavy VPS workloads shows what the upgrade path looks like when a 2 GB box stops being enough. Until then, the settings in this guide keep PostgreSQL 17 fast and stable on the server you already have.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.