A VPS for PostgreSQL: memory, the WAL and backups
PostgreSQL is undemanding: it will start even on a server with a gigabyte of memory. But the default settings are designed for a very modest machine, and after installing it on your own VPS you almost always need to adjust a few parameters. Below is what to look for in a plan so the database doesn't hit the limits of the hardware, and what to configure on day one.
Memory: shared_buffers, the OS cache and work_mem
PostgreSQL reads data through two caches at once: its own (shared_buffers) and the operating system's file cache. That's why the documentation advises starting with a quarter of memory for shared_buffers and not giving it more than about 40%: the kernel uses the rest to cache the same files anyway. The effective_cache_size parameter doesn't allocate memory; it only tells the planner how much data is probably already in the cache.
The most dangerous one is work_mem. It is a limit not per connection but per sort or hash operation inside a query, and for hash operations it is also multiplied by hash_mem_multiplier (2 by default). A complex report with three sorts across twenty connections can easily take tens of times more than the config says, and the server runs out of memory. It's safer to keep a moderate value globally and raise it for heavy reports with SET work_mem in a specific session. Enable log_temp_files = 0: the log will show queries that ran out of memory and are sorting on disk.
This gives a simple rule for choosing a plan: memory should be enough for the hot part of your tables and indexes plus headroom for connections. If the working set grows, move to a VPS with 16 GB of RAM rather than adding cores.
Disk: the WAL, fsync and write latency
Every confirmed commit is a write to the WAL plus an fsync call that waits until the data physically reaches the drive. Throughput in megabytes barely matters here; what matters is the latency of a single synchronous write. On an overloaded array it can reach tens of milliseconds, and then an application with frequent small transactions is bottlenecked by the disk while the CPU sits almost idle. So for a production database take NVMe plans, and after buying, test the disk with the pg_test_fsync utility that ships with PostgreSQL: it shows how many synchronous writes per second the server can really sustain.
Never turn off fsync for speed: after a power failure or a host reboot the cluster can end up corrupted beyond recovery. If you need fast commits and losing the last fractions of a second is acceptable, there is a safe alternative, synchronous_commit = off: the database stays consistent, and only the most recent transactions are lost. If the plan lets you attach a second disk, you can move the log to a separate volume (the pg_wal directory or the initdb --waldir option) so that WAL writes don't compete with table reads.
Keep an eye on checkpoints too. With the default max_wal_size of 1 GB, heavy writing triggers checkpoints too often, and latency rises in a sawtooth pattern every few minutes. Increase it and checkpoint_timeout, and watch the log for warnings about checkpoints occurring too frequently.
Backups: pg_dump or PITR
- pg_dump makes a logical copy of one database as of the moment the dump starts. It suits databases up to tens of gigabytes and moves between PostgreSQL versions, but you can only restore to the moment of the dump: everything written afterwards is lost.
- PITR is a base backup via
pg_basebackupplus a continuous WAL archive (archive_command). It lets you roll back to any second, for example to the moment before a mistakenDELETE. It's easiest with ready-made tools such as pgBackRest or WAL-G.
Store the WAL archive and backups off the database server, in object storage or on a separate VPS for backups. And bear in mind that with archiving enabled, unsent WAL segments pile up locally: if the storage is unreachable, the database disk will fill up and PostgreSQL will stop.
Replicas: when you need one and what eats disk space
Streaming replication gives you a hot copy on a second server: you fail over to it if the primary goes down and send heavy read queries to it. Put the replica in a different data centre, or at least with a different provider, otherwise an outage at one site will take out both machines. Connect the servers only over a private network or WireGuard.
The main trap is replication slots. A slot keeps WAL until the replica fetches it, and a disconnected replica quietly accumulates gigabytes of log on the primary. Cap the growth with max_slot_wal_keep_size and set up alerts on free space and replica lag; for collecting metrics, see the VPS for monitoring page.