Run a production database server on bare metal
A dedicated database server hands you the whole machine: every core, every gigabyte of RAM and every NVMe IOPS belongs to your database and nothing else. This guide is a PostgreSQL production setup you can follow top to bottom on Ubuntu 24.04 LTS, from install and tuned config to access control, backups that actually restore, and a streaming replica.
The worked example is PostgreSQL 16, but the reasoning behind PostgreSQL bare metal tuning carries over to MySQL and MariaDB: give the engine real memory, fast local disks, and settings that match the hardware, then prove your backups. Server.Biz servers are unmanaged, so you are the DBA here. Every command below is yours to run.
Follow it in order and you end with a self-hosted database server that is private by default, patched, backed up, and monitored.
Why databases love bare metal
Virtual database instances share physical disks and CPUs with strangers. Your p99 latency then depends on what the neighbour is doing: a backup job or a busy tenant on the same host steals IOPS and CPU cycles you were billed for. On a dedicated server there is no noisy neighbour and no CPU steal. The cores are yours, the NVMe queue is yours, and latency stays flat under load.
Memory is the other big win. A database is happiest when its working set lives in RAM, and on bare metal all of the RAM is yours to give to the cache. There is no hypervisor ballooning and no per-instance memory cap deciding how much your buffer pool may grow to.
When a managed database is the better call
Managed databases still win in two honest cases: when nobody on your team has time to be a DBA, and when you need one-click high availability with automatic failover. If an afternoon of patching or a 3 a.m. restore is not something you can own, a managed service is the right choice. This guide assumes you want the control and the economics of running it yourself.
Hardware and storage layout
Start with mirrored NVMe. RAID 1 for a single pair, RAID 10 once you have four or more drives, gives you a database volume that survives a disk failure without the long, performance-crushing rebuild that parity arrays suffer. Our RAID guide covers the tradeoffs. For databases the answer is almost always mirror, not parity.
Separating the write-ahead log (WAL) onto its own disk only pays off on constrained or spinning disks. On fast NVMe the WAL and the data files share one volume comfortably, so do not add a layout you will not benefit from.
Size RAM around your working set: the portion of data your day-to-day queries actually touch. If that set fits in RAM, reads rarely hit disk and latency stays low. Pick enough memory to hold the working set with headroom for the operating system and connections. Our database server category lists machines with the NVMe and RAM balance this needs.
Before you install: harden the box
A database server should never be the machine where you learn Linux hardening. Before PostgreSQL touches the disk, do the first-hour basics: change the delivered root password, create an admin user with an SSH key, disable password logins, patch fully, and turn on a default-deny firewall. Our getting-your-server-online guide is that checklist; follow it first, then come back here.
The one database-specific rule to hold in your head: the database port never faces the public internet. It listens on localhost or a private VLAN only, and the host firewall backs that up.
Install PostgreSQL 16 from the PGDG repository
Ubuntu ships an older PostgreSQL in its default repositories. Use the official PostgreSQL APT repository (PGDG) so you get 16.x and timely security patches. These commands are for Ubuntu 24.04 LTS.
Add the PGDG key and repository, then install the server package.
sudo apt update
sudo apt install -y curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail \
https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
| sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt install -y postgresql-16
Ubuntu auto-creates a cluster using the system locale. For deterministic sorting and comparisons, recreate it with the C.UTF-8 locale and turn on data checksums, which catch silent disk corruption early. Do this before you load any data.
sudo pg_dropcluster --stop 16 main
sudo pg_createcluster --locale=C.UTF-8 16 main -- --data-checksums
sudo systemctl enable --now postgresql@16-main
The dozen settings that matter
PostgreSQL ships with conservative defaults that assume a small, shared machine. On a dedicated box you tell it the truth about the hardware. The file below drops into the config include directory that Ubuntu already loads (conf.d), and it is sized for a 64 GB RAM machine on NVMe.
Create this one file. Every setting has a one-line reason next to it.
# --- memory ---
shared_buffers = 16GB # ~1/4 of RAM: PostgreSQL's own page cache
effective_cache_size = 48GB # ~3/4 of RAM: hint of OS + PG cache, favours index scans
work_mem = 64MB # per sort/hash node; parallelism multiplies this, keep it modest
maintenance_work_mem = 2GB # speeds up VACUUM, CREATE INDEX, and restores
# --- write-ahead log & checkpoints ---
wal_compression = on # smaller WAL, less IO, cheaper archiving
max_wal_size = 8GB # room between checkpoints so writes are not forced early
min_wal_size = 2GB
checkpoint_completion_target = 0.9 # spread checkpoint writes to avoid IO spikes
# --- planner for NVMe ---
random_page_cost = 1.1 # random reads on NVMe cost almost the same as sequential
effective_io_concurrency = 256 # let the planner issue many concurrent reads
# --- parallelism (16-core class box) ---
max_worker_processes = 16
max_parallel_workers = 12
max_parallel_workers_per_gather = 4
# --- kernel ---
huge_pages = try # use 2MB pages when available, less page-table overhead
# --- keep it private ---
listen_addresses = 'localhost' # or a private VLAN address; never a public IP or '*'
shared_buffers, huge_pages and the WAL sizes need a full restart, not a reload. Apply them now.
sudo systemctl restart postgresql@16-main
shared_buffers is PostgreSQL's own cache and a quarter of RAM is a safe start; effective_cache_size at three quarters tells the planner how much the OS is also caching, so it prefers index scans. work_mem is allocated per sort or hash, so keep it modest and let parallel workers scale throughput. random_page_cost at 1.1 and effective_io_concurrency at 256 are the two settings that make the planner treat NVMe as the fast random-access device it is.
For other memory sizes, scale shared_buffers and effective_cache_size proportionally, or paste your specs into a pgtune-style calculator and adjust from there. The ratios matter more than the exact numbers.
Access control and a least-privilege app role
PostgreSQL 16 uses SCRAM-SHA-256 password hashing by default. Two things control access: pg_hba.conf decides which host, user and database combinations may connect and how they authenticate, and the role system decides what they can do once in.
Keep this file tight. The hostssl line requires an encrypted connection from the application subnet only; replace 10.0.0.0/24 with your private VLAN range and appdb/appuser with your names.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
hostssl appdb appuser 10.0.0.0/24 scram-sha-256
Reload to apply the file, then create the application role. It owns its own database but is not a superuser and cannot create other databases or roles.
sudo systemctl reload postgresql@16-main
sudo -u postgres psql <<'SQL'
CREATE ROLE appuser LOGIN PASSWORD 'set-a-real-secret-from-your-vault';
CREATE DATABASE appdb OWNER appuser;
\c appdb
REVOKE ALL ON SCHEMA public FROM PUBLIC;
GRANT USAGE, CREATE ON SCHEMA public TO appuser;
SQL
On Ubuntu, PostgreSQL enables TLS with a self-signed certificate out of the box, which is why the hostssl rule works immediately. For production, replace that certificate with a real one, or keep database traffic on a private VLAN where the datacenter partner's network isolates it from the public internet. Do not put the database password in application source; read it from a secrets manager or an environment variable.
Backups that actually restore
A backup you have never restored is a rumour. pgBackRest gives you full, differential and incremental backups with WAL archiving for point-in-time recovery, and it turns a restore into a single command. Install it, point it at the cluster, archive WAL, and then rehearse the restore.
Install pgBackRest from the Ubuntu repositories.
sudo apt install -y pgbackrest
Configure a local repository. process-max and zstd compression keep backups fast; retention keeps two full backups and the WAL between them.
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
process-max=4
compress-type=zst
start-fast=y
[main]
pg1-path=/var/lib/postgresql/16/main
Turn on WAL archiving, restart to apply archive_mode, then create the stanza and take the first full backup.
sudo -u postgres psql -c "ALTER SYSTEM SET archive_mode = on;"
sudo -u postgres psql -c "ALTER SYSTEM SET archive_command = 'pgbackrest --stanza=main archive-push %p';"
sudo systemctl restart postgresql@16-main
sudo -u postgres pgbackrest --stanza=main stanza-create
sudo -u postgres pgbackrest --stanza=main check
sudo -u postgres pgbackrest --stanza=main --type=full backup
Schedule a weekly full and daily differential backups. Backups that depend on someone remembering do not happen.
30 2 * * 0 postgres pgbackrest --stanza=main --type=full backup
30 2 * * 1-6 postgres pgbackrest --stanza=main --type=diff backup
The non-negotiable part: restore to a scratch path, start it on a spare port, and query it. Run this drill on a schedule, not once. archive-mode=off stops the test instance from touching your live WAL.
sudo -u postgres pgbackrest --stanza=main \
--pg1-path=/var/lib/postgresql/16/restore-test \
--archive-mode=off restore
sudo -u postgres /usr/lib/postgresql/16/bin/pg_ctl \
-D /var/lib/postgresql/16/restore-test -o "-p 5433" start
sudo -u postgres psql -p 5433 -c "SELECT now(), count(*) FROM pg_stat_user_tables;"
sudo -u postgres /usr/lib/postgresql/16/bin/pg_ctl \
-D /var/lib/postgresql/16/restore-test stop
A local repository protects against a lost data file, not against losing the whole machine. Add an offsite copy: pgBackRest can push a second repository to object storage or to a separate backup box. Our backup and storage servers are built for exactly that role.
A streaming replica in six commands
A streaming replica keeps a second server continuously in sync with the primary. It protects against the primary's hardware dying and it gives you a read scaling target. It is not a backup: a bad DELETE replicates to the standby in milliseconds. Keep pgBackRest running alongside it.
On the primary, create a replication role and allow the standby to connect. Replace 10.0.0.6 with the standby's private address.
sudo -u postgres psql -c "CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'a-strong-secret';"
# Add this line to /etc/postgresql/16/main/pg_hba.conf, then reload:
# hostssl replication replicator 10.0.0.6/32 scram-sha-256
sudo systemctl reload postgresql@16-main
On the standby, clone the primary with pg_basebackup. The -R flag writes the standby signal and connection settings for you. Export PGPASSWORD (or use a .pgpass file) so the clone can authenticate; replace 10.0.0.5 with the primary's address.
sudo systemctl stop postgresql@16-main
sudo -u postgres rm -rf /var/lib/postgresql/16/main
sudo -u postgres PGPASSWORD='a-strong-secret' pg_basebackup \
-h 10.0.0.5 -U replicator -D /var/lib/postgresql/16/main -Fp -Xs -R -P
sudo systemctl start postgresql@16-main
If the primary fails, promote the standby in one command: sudo pg_ctlcluster 16 main promote. Point your application at the new primary and rebuild a fresh standby from it when the dust settles.
Monitoring and autovacuum
You cannot tune what you cannot see. Turn on pg_stat_statements to find your heaviest queries, log the slow ones, and watch disk space, because a failed archive_command can silently fill the WAL directory and stop the database.
Add this file, restart once (shared_preload_libraries needs it), then run CREATE EXTENSION pg_stat_statements; in each database you want to track.
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 10000
pg_stat_statements.track = top
log_min_duration_statement = 500ms # log any statement slower than half a second
# autovacuum: never disable it, tune it for large, write-heavy tables
autovacuum_max_workers = 4
autovacuum_vacuum_scale_factor = 0.05
autovacuum_analyze_scale_factor = 0.02
Point your existing monitoring (node exporter, or whatever agent you already run) at free disk space and alert well before the data or WAL volume fills, say at three quarters full. Leave autovacuum on: it reclaims dead rows and keeps the planner's statistics current. Disabling it is how tables bloat and queries slowly rot. On busy tables, lower the scale factors as above so vacuum runs more often on smaller deltas.
Managed cloud database vs self-run on dedicated
| Managed cloud database | Self-run on dedicated | |
|---|---|---|
| Performance | Shared tenancy, metered and capped IOPS | Dedicated NVMe and RAM, no CPU steal |
| High availability | One-click, provider-run failover | You build streaming replication and failover |
| Backups | Automated, included, provider-owned | You run pgBackRest and test the restores |
| Version and extensions | Provider-limited menu | Any version, any extension |
| Operational burden | Minimal, you barely touch it | You are the DBA |
| Best when | No DBA time, spiky or unpredictable load | Steady load, cost control, full control |
The honest bottom line
If nobody owns database operations, pay for managed and move on. If your load is steady and you want the hardware, the tuning and the backups under your own control, a dedicated database server is faster and cheaper to run, and everything above gets you there. Server.Biz sells the unmanaged machine; the setup is yours. Browse the database server category to pick the NVMe and RAM that fit your working set.
