Running a database server on a VPS: memory, exposure and dumps

A database on a VPS goes wrong in three predictable ways: it has default memory settings, its port answers to the whole internet, or its dumps have never been restored. Fix those three and a plain PostgreSQL or MySQL on 4–8 GB of RAM will carry most small company systems.

Updated: · Prices in the text are with the public IP (₼10 a month) unless stated otherwise.

Same server as the app, or its own

Keep both on one machine while the data fits in memory next to the application and one restart of the whole thing is acceptable. Move the database out when any of these is true:

  • the application and the database fight for RAM (swap in use, the kernel killing processes);
  • two or more applications need the same data;
  • you need different people to have access to code and to data;
  • you want to rebuild the app server without touching the data.

When you split, order the machines together so that they share a private subnet, and use the private addresses between them. Each server also has its own public IPv4 (₼10 a month), so on the database machine the firewall has to deny everything that arrives there. If you want it to have no public address at all, a private network VPS stands behind your own router: the server price without the public IP (₼10 less) plus ₼35 a month for a new private network and router. The private network guide draws that layout.

How much memory

Databases are fast when the part of the data you use every day stays in RAM. Find its size first:

sudo -u postgres psql -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) FROM pg_database;"
sudo mysql -e "SELECT table_schema, ROUND(SUM(data_length+index_length)/1024/1024) AS mb FROM information_schema.tables GROUP BY 1;"
Total dataTypical hot partStarting plan
Up to 2 GBNearly all of itStandard (2 vCPU, 4 GB RAM, 60 GB NVMe)
2–15 GB2–5 GBProfessional (4 vCPU, 8 GB RAM, 100 GB NVMe)
15–50 GB5–12 GBBusiness Pro (8 vCPU, 16 GB RAM, 160 GB NVMe)
Over 50 GB12–25 GBEnterprise (12 vCPU, 32 GB RAM, 240 GB NVMe); above 80 GB of data Enterprise Pro (16 vCPU, 48 GB RAM, 320 GB NVMe)

These are starting points. Disk must hold the data, its indexes, temporary files and at least one fresh dump, so plan for three times the data size. If your need falls between two rows, take the smallest plan that covers both memory and disk: 12 GB of RAM and 120 GB of disk is Business at ₼105.90 a month with public IP, and the same memory with 200 GB of disk already means Premium (8 vCPU, 24 GB RAM, 200 GB NVMe) at ₼179.90.

  • Standard₼39.90/mo

    2 vCPU · 4 GB RAM · 60 GB NVMe

    Public IP +₼10/mo, With public IP: ₼49.90/mo

  • Professional₼73.90/mo

    4 vCPU · 8 GB RAM · 100 GB NVMe

    Public IP +₼10/mo, With public IP: ₼83.90/mo

  • Business Pro₼145.90/mo

    8 vCPU · 16 GB RAM · 160 GB NVMe

    Public IP +₼10/mo, With public IP: ₼155.90/mo

Prices in manat (AZN), billed monthly. The public IP is +₼10 a month and is assigned automatically.

Two machines cost two plans: Basic for a light application and Professional for the database come to ₼121.80 a month, each with its public IP. If app and database share one machine, compare that with a single Business at ₼105.90, which also leaves you one system to update.

PostgreSQL settings worth changing

Ubuntu 24.04 ships PostgreSQL 16 and Debian 12 ships 15. Put your values into their own file, for example /etc/postgresql/16/main/conf.d/tuning.conf, for a dedicated server with 8 GB:

shared_buffers = 2GB
effective_cache_size = 6GB
work_mem = 16MB
maintenance_work_mem = 512MB
max_connections = 100
random_page_cost = 1.1
log_min_duration_statement = 500

For 4 GB halve the first two, set work_mem to 8MB and maintenance_work_mem to 256MB. For 16 GB double the first two.

The dangerous one is work_mem: it applies to each sort or hash in each query, not to the server. One hundred connections with two sorts each at 16 MB could ask for about 3.2 GB on top of shared_buffers. If your application opens hundreds of connections, put PgBouncer in front rather than raising max_connections.

Restart (systemctl restart postgresql), let it work for a day, then check how often reads are served from memory:

SELECT round(100.0 * sum(blks_hit) / nullif(sum(blks_hit + blks_read), 0), 2) AS hit_percent
FROM pg_stat_database;

A transactional system that stays under about 95 percent usually needs more RAM or a missing index.

MySQL and MariaDB settings

Ubuntu 24.04 packages MySQL 8.0; Debian 12 packages MariaDB 10.11. The buffer pool does nearly all the work. For a dedicated 8 GB machine, in /etc/mysql/mysql.conf.d/zz-tuning.cnf (MariaDB: /etc/mysql/mariadb.conf.d/zz-tuning.cnf):

[mysqld]
innodb_buffer_pool_size = 5G
max_connections = 150
slow_query_log = 1
long_query_time = 0.5

On MySQL 8.0.30 or newer add innodb_redo_log_capacity = 1G if you write a lot. Leave the old query_cache advice alone: MySQL 8 removed that feature and refuses to start with it. If the application shares the machine, give the pool 40 to 50 percent of RAM instead of 60 to 70.

Keep the port off the internet

Port 5432 or 3306 open to the world receives password guesses within hours. Three safe patterns, best first:

  1. Private subnet. Listen on the private address only and allow only the app server. The machine also has a public IPv4, so run ufw default deny incoming and allow SSH from the app server before ufw enable.
  2. VPN for people; see the company VPN setup.
  3. SSH tunnel through the app server, for one administrator.
# tunnel: local port 5433 leads to the database through the app server
ssh -N -L 5433:10.20.0.5:5432 admin@app.example.com
# firewall on the database server: only the app server
ufw allow from 10.20.0.4 to any port 5432 proto tcp

PostgreSQL needs listen_addresses = 'localhost,10.20.0.5' and one line in pg_hba.conf:

hostssl  shop  shop_app  10.20.0.4/32  scram-sha-256

In MySQL set bind-address = 10.20.0.5 and create the user for one host: CREATE USER 'shop_app'@'10.20.0.4' IDENTIFIED BY '...';. Grant it rights on its own database only; the application never connects as postgres or root.

Updates when outgoing traffic is closed

A server with its own public IPv4 downloads packages directly even when every incoming port is denied. If you have blocked outgoing traffic as well, or the database is a private network VPS whose router lets nothing out, lend it your connection for the length of an SSH session that goes through the app server:

# on your laptop: connect through the app server and share a SOCKS proxy
ssh -J admin@app.example.com -R 1080 admin@10.20.0.5
# on the database server, inside that session
echo 'Acquire::http::Proxy "socks5h://127.0.0.1:1080";' | sudo tee /etc/apt/apt.conf.d/99tunnel
sudo apt update && sudo apt upgrade

Dumps, and the restore that proves them

VPS plans do not come with automatic backups, so schedule your own. Save as /usr/local/sbin/db-dump.sh, make it executable and run it as root:

#!/bin/bash
set -euo pipefail
cd /
umask 077
DIR=/var/backups/pg
STAMP=$(date +%F-%H%M)
install -d -o postgres -g postgres -m 700 "$DIR"
sudo -u postgres pg_dumpall --globals-only > "$DIR/globals-$STAMP.sql"
sudo -u postgres pg_dump -Fc -f "$DIR/shop-$STAMP.dump" shop
find "$DIR" -type f -mtime +7 -delete

For InnoDB tables in MySQL, mysqldump --single-transaction --routines --events shop | gzip > shop.sql.gz takes a consistent copy without stopping writes. Keep copies in another place. It is safer if the database server holds no storage keys: let the app server fetch the files over the private subnet and send them on, for example with the steps in VPS backup to S3.

Once a month restore the newest file into a scratch database and look at it:

sudo -u postgres createdb restore_test
F=$(ls -t /var/backups/pg/shop-*.dump | head -1)
sudo -u postgres pg_restore --exit-on-error -d restore_test "$F"
sudo -u postgres psql -d restore_test -c "SELECT max(created_at) FROM orders;"
sudo -u postgres dropdb restore_test

Replace the query with something a manager would recognise, such as the last order. Write down how long the restore took: that is your real recovery time. If losing a day of entries is unacceptable, dumps are not enough and you need WAL archiving (PostgreSQL) or binary logs (MySQL) for point-in-time recovery.

Database server questions

Can I run PostgreSQL on a VPS with 1 GB of RAM?

For a small internal tool, yes, with shared_buffers around 256MB and few connections. MySQL 8 is tight at 1 GB; take 2 GB or more.

Does a database server need a public IP?

Not for its work: the application and administrators reach it over the private subnet. Every ordinary Cloudraft VPS has one anyway (₼10 a month), so deny all incoming traffic on it and bind the database to the private address. Only a private network VPS has none; it stands behind your own router.

Should the database run in Docker?

It works, as long as the data is in a named volume and you dump it from inside the container. For a single database, the distribution package is easier to update and to tune.

How do I move a live database to a new server?

Test a dump and restore first to learn the time. Then announce a window, stop the application, take the final dump, restore it, compare row counts and switch the connection string. For near-zero downtime use replication.

Read next

All VPS pages

This page in other languages: Azərbaycanca · Русский

Servers in Baku, prices in manat

Create an account, top up your balance and order the plan you need.