2024-09-21 22:19:00 +01:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
inputs,
|
|
|
|
config,
|
|
|
|
...
|
|
|
|
}: let
|
2024-07-06 16:02:10 +01:00
|
|
|
admin_dbPasswordFile = lib.optionalString (lib.hasAttr "sops-nix" inputs) config.sops.secrets."software/postgres/admin_db/password".path;
|
2024-07-06 20:53:26 +01:00
|
|
|
initScript = pkgs.writeText "init.sh" ''
|
|
|
|
#!/bin/bash
|
|
|
|
function create_user_and_database() {
|
|
|
|
local database=$1
|
|
|
|
local user=$2
|
|
|
|
local extensions=$3
|
|
|
|
echo "### admin user: $POSTGRES_USER ###"
|
|
|
|
echo " Creating database '$database'"
|
|
|
|
echo " Creating user '$user'"
|
|
|
|
psql -v --username "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
|
|
|
|
CREATE USER $user;
|
|
|
|
CREATE DATABASE $database;
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE $database TO $user;
|
|
|
|
EOSQL
|
2024-09-21 22:19:00 +01:00
|
|
|
|
2024-07-06 20:53:26 +01:00
|
|
|
# Loop through extensions and create them
|
|
|
|
for ext in $(echo "$extensions" | tr ',' ' '); do
|
|
|
|
echo " - Installing extention $ext"
|
|
|
|
psql -v --username "$POSTGRES_USER" -d "$database" -c "CREATE EXTENSION $ext;"
|
|
|
|
done
|
|
|
|
}
|
2024-09-21 22:19:00 +01:00
|
|
|
|
2024-07-06 20:53:26 +01:00
|
|
|
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
|
2024-09-21 22:19:00 +01:00
|
|
|
|
2024-07-06 20:53:26 +01:00
|
|
|
# Parse the JSON string
|
|
|
|
database_names=$(echo "$POSTGRES_MULTIPLE_DATABASES" | jq -r '.[0] | keys[]')
|
|
|
|
echo "Multiple database creation requested: $(echo "$database_names" | tr "\n" " ")"
|
2024-09-21 22:19:00 +01:00
|
|
|
|
2024-07-06 20:53:26 +01:00
|
|
|
# Loop through each database and create it
|
|
|
|
for db_name in $database_names; do
|
|
|
|
user=$(echo "$POSTGRES_MULTIPLE_DATABASES" | jq -r ".[0] | .''${db_name} | .user")
|
|
|
|
extensions=$(echo "$POSTGRES_MULTIPLE_DATABASES" | jq -r ".[0] | .''${db_name} | .extensions | join(\",\")")
|
|
|
|
create_user_and_database "$db_name" "$user" "$extensions"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
pg_hbaConfig = pkgs.writeText "pg_hba.conf" ''
|
|
|
|
none
|
|
|
|
'';
|
|
|
|
|
|
|
|
pgsqlConfig = pkgs.writeText "postgresql.conf" ''
|
|
|
|
listen_addresses = '*'
|
|
|
|
port = 5432
|
|
|
|
max_connections = 100
|
|
|
|
shared_buffers = 24GB
|
|
|
|
work_mem = 1GB
|
|
|
|
maintenance_work_mem = 10GB
|
|
|
|
autovacuum_work_mem = 2GB
|
|
|
|
dynamic_shared_memory_type = posix
|
|
|
|
wal_level = minimal
|
|
|
|
checkpoint_timeout = 60min
|
|
|
|
checkpoint_completion_target = 0.9
|
|
|
|
max_wal_size = 10GB
|
|
|
|
min_wal_size = 80MB
|
|
|
|
max_wal_senders = 0
|
|
|
|
random_page_cost = 1.0
|
|
|
|
effective_cache_size = 25GB
|
|
|
|
jit = off
|
|
|
|
log_line_prefix = '%m [%p] %q%u@%d '
|
|
|
|
log_timezone = 'Etc/UTC'
|
|
|
|
cluster_name = 'postgres-docker'
|
|
|
|
datestyle = 'iso, dmy'
|
|
|
|
timezone = 'Etc/UTC'
|
|
|
|
default_text_search_config = 'pg_catalog.english'
|
|
|
|
'';
|
2024-09-21 22:19:00 +01:00
|
|
|
in {
|
2024-07-06 16:02:10 +01:00
|
|
|
sops.secrets = {
|
2024-09-21 22:19:00 +01:00
|
|
|
"software/postgres/admin_db/password" = {};
|
2024-07-06 16:02:10 +01:00
|
|
|
};
|
2024-07-05 18:58:03 +01:00
|
|
|
virtualisation.arion = {
|
|
|
|
backend = "docker";
|
|
|
|
projects = {
|
|
|
|
"db".settings.services."db".service = {
|
|
|
|
restart = "unless-stopped";
|
2024-07-06 10:26:08 +01:00
|
|
|
build.context = "/nix/store";
|
2024-07-06 20:53:26 +01:00
|
|
|
build.dockerfile = builtins.baseNameOf "${pkgs.writeText "pgDockerfile" ''
|
2024-09-21 22:19:00 +01:00
|
|
|
FROM postgres:16
|
|
|
|
# install packages
|
|
|
|
RUN apt-get update \
|
|
|
|
&& apt-get install -y --no-install-recommends \
|
|
|
|
postgresql-16-postgis \
|
|
|
|
jq \
|
|
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
''}";
|
|
|
|
command = ["postgres" "-c" "config_file=/etc/postgresql/postgresql.conf"];
|
2024-07-05 18:58:03 +01:00
|
|
|
environment = {
|
2024-07-06 16:02:10 +01:00
|
|
|
POSTGRES_PASSWORD_FILE = admin_dbPasswordFile;
|
2024-07-05 18:58:03 +01:00
|
|
|
POSTGRES_USER = "admin";
|
2024-07-06 10:27:15 +01:00
|
|
|
POSTGRES_DB = "admin_db";
|
|
|
|
PGDATA = "/var/lib/postgresql/data/pgdata";
|
2024-07-06 16:02:10 +01:00
|
|
|
POSTGRES_MULTIPLE_DATABASES = ''
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"osm": {
|
|
|
|
"user": "gis",
|
|
|
|
"extensions": [
|
|
|
|
"hstore",
|
|
|
|
"postgis"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"bitcoin": {
|
|
|
|
"user": "satoshi",
|
|
|
|
"extensions": []
|
|
|
|
},
|
|
|
|
"btc_models": {
|
|
|
|
"user": "dbt",
|
|
|
|
"extensions": []
|
|
|
|
},
|
|
|
|
"dev_btc_models": {
|
|
|
|
"user": "dbt",
|
|
|
|
"extensions": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2024-07-05 18:58:03 +01:00
|
|
|
};
|
2024-09-21 22:19:00 +01:00
|
|
|
ports = ["5432:5432"];
|
2024-07-05 18:58:03 +01:00
|
|
|
volumes = [
|
2024-07-06 20:53:26 +01:00
|
|
|
# Mount pgdata to external zfs volume
|
2024-07-06 10:28:09 +01:00
|
|
|
"/mnt/postgres:/var/lib/postgresql/data"
|
|
|
|
|
2024-07-06 20:53:26 +01:00
|
|
|
# Mount config files
|
2024-07-06 21:17:32 +01:00
|
|
|
# "${pg_hbaConfig}:/var/lib/postgres/data/pgdata/pg_hba.conf"
|
2024-07-06 20:53:26 +01:00
|
|
|
"${pgsqlConfig}:/etc/postgresql/postgresql.conf"
|
|
|
|
|
2024-09-21 22:19:00 +01:00
|
|
|
# Need to mount secret file
|
2024-07-06 16:02:10 +01:00
|
|
|
"${admin_dbPasswordFile}:${admin_dbPasswordFile}"
|
|
|
|
|
|
|
|
# PG init script to parse json specified in POSTGRES_MULTIPLE_DATABASES
|
|
|
|
# creates databases, users and installs extensions for each database.
|
2024-07-06 20:53:26 +01:00
|
|
|
"${initScript}:/docker-entrypoint-initdb.d/init.sh"
|
2024-07-05 18:58:03 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|