2023-08-16 11:09:22 +00:00
|
|
|
{ pkgs, ... }:
|
2022-04-27 06:38:39 +00:00
|
|
|
|
2023-08-17 01:26:30 +00:00
|
|
|
let
|
|
|
|
GiB = n: MiB 1024*n;
|
|
|
|
MiB = n: KiB 1024*n;
|
|
|
|
KiB = n: 1024*n;
|
|
|
|
in
|
2022-04-27 06:38:39 +00:00
|
|
|
{
|
2023-01-06 11:29:13 +00:00
|
|
|
sane.persist.sys.plaintext = [
|
2022-07-11 00:58:16 +00:00
|
|
|
# TODO: mode?
|
2023-07-08 00:56:20 +00:00
|
|
|
{ user = "postgres"; group = "postgres"; path = "/var/lib/postgresql"; }
|
2022-07-11 00:58:16 +00:00
|
|
|
];
|
2022-04-27 06:38:39 +00:00
|
|
|
services.postgresql.enable = true;
|
2023-08-16 11:09:22 +00:00
|
|
|
|
|
|
|
# HOW TO UPDATE:
|
|
|
|
# postgres version updates are manual and require intervention.
|
|
|
|
# - `sane-stop-all-servo`
|
|
|
|
# - `systemctl start postgresql`
|
|
|
|
# - as `sudo su postgres`:
|
|
|
|
# - `cd /var/log/postgresql`
|
|
|
|
# - `pg_dumpall > state.sql`
|
|
|
|
# - `echo placeholder > <new_version>` # to prevent state from being created earlier than we want
|
|
|
|
# - then, atomically:
|
|
|
|
# - update the `services.postgresql.package` here
|
|
|
|
# - `dataDir` is atomically updated to match package; don't touch
|
|
|
|
# - `nixos-rebuild --flake . switch ; sane-stop-all-servo`
|
|
|
|
# - `sudo rm -rf /var/lib/postgresql/<new_version>`
|
|
|
|
# - `systemctl start postgresql`
|
|
|
|
# - as `sudo su postgres`:
|
|
|
|
# - `cd /var/lib/postgreql`
|
|
|
|
# - `psql -f state.sql`
|
|
|
|
# - restart dependent services (maybe test one at a time)
|
|
|
|
|
|
|
|
services.postgresql.package = pkgs.postgresql_15;
|
|
|
|
|
|
|
|
|
2022-04-27 06:38:39 +00:00
|
|
|
# XXX colin: for a proper deploy, we'd want to include something for Pleroma here too.
|
|
|
|
# services.postgresql.initialScript = pkgs.writeText "synapse-init.sql" ''
|
|
|
|
# CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD '<password goes here>';
|
|
|
|
# CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
|
|
|
|
# TEMPLATE template0
|
|
|
|
# ENCODING = "UTF8"
|
|
|
|
# LC_COLLATE = "C"
|
|
|
|
# LC_CTYPE = "C";
|
|
|
|
# '';
|
2022-05-11 06:41:34 +00:00
|
|
|
|
2023-08-17 01:26:30 +00:00
|
|
|
# perf tuning
|
2022-11-11 10:58:57 +00:00
|
|
|
# - for recommended values see: <https://pgtune.leopard.in.ua/>
|
|
|
|
# - for official docs (sparse), see: <https://www.postgresql.org/docs/11/config-setting.html#CONFIG-SETTING-CONFIGURATION-FILE>
|
2023-08-17 01:26:30 +00:00
|
|
|
services.postgresql.settings = {
|
|
|
|
# DB Version: 15
|
|
|
|
# OS Type: linux
|
|
|
|
# DB Type: web
|
|
|
|
# Total Memory (RAM): 32 GB
|
|
|
|
# CPUs num: 12
|
|
|
|
# Data Storage: ssd
|
|
|
|
max_connections = 200;
|
|
|
|
shared_buffers = "8GB";
|
|
|
|
effective_cache_size = "24GB";
|
|
|
|
maintenance_work_mem = "2GB";
|
|
|
|
checkpoint_completion_target = 0.9;
|
|
|
|
wal_buffers = "16MB";
|
|
|
|
default_statistics_target = 100;
|
|
|
|
random_page_cost = 1.1;
|
|
|
|
effective_io_concurrency = 200;
|
|
|
|
work_mem = "10485kB";
|
|
|
|
min_wal_size = "1GB";
|
|
|
|
max_wal_size = "4GB";
|
|
|
|
max_worker_processes = 12;
|
|
|
|
max_parallel_workers_per_gather = 4;
|
|
|
|
max_parallel_workers = 12;
|
|
|
|
max_parallel_maintenance_workers = 4;
|
|
|
|
};
|
2022-11-11 10:58:57 +00:00
|
|
|
|
2022-05-18 10:42:47 +00:00
|
|
|
# daily backups to /var/backup
|
|
|
|
services.postgresqlBackup.enable = true;
|
2022-05-11 06:41:34 +00:00
|
|
|
|
|
|
|
# common admin operations:
|
1980-01-01 00:54:42 +00:00
|
|
|
# sudo systemctl start postgresql
|
2022-05-11 06:41:34 +00:00
|
|
|
# sudo -u postgres psql
|
|
|
|
# > \l # lists all databases
|
|
|
|
# > \du # lists all roles
|
1980-01-01 00:54:42 +00:00
|
|
|
# > \c pleroma # connects to database by name
|
|
|
|
# > \d # shows all tables
|
2022-05-11 06:41:34 +00:00
|
|
|
# > \q # exits psql
|
1980-01-01 00:54:42 +00:00
|
|
|
# dump/restore (-F t = tar):
|
|
|
|
# sudo -u postgres pg_dump -F t pleroma > /backup/pleroma-db.tar
|
|
|
|
# sudo -u postgres -g postgres pg_restore -d pleroma /backup/pleroma-db.tar
|
2022-04-27 06:38:39 +00:00
|
|
|
}
|