fix postgres init script

This commit is contained in:
2025-03-17 11:19:48 +04:00
parent b53f6f65ef
commit 82930f63f3

39
lib.nix
View File

@@ -66,6 +66,8 @@ in
extra_config ? '''', extra_config ? '''',
package ? pkgs.postgresql, package ? pkgs.postgresql,
data_dir ? ".data/", data_dir ? ".data/",
port ? 5432,
listen_addresses ? "localhost",
... ...
}: }:
let let
@@ -83,7 +85,11 @@ in
else else
{ } { }
) )
// config; // config
// {
unix_socket_directories = data_dir;
inherit port listen_addresses;
};
toStr = toStr =
value: value:
if true == value then if true == value then
@@ -102,15 +108,36 @@ in
) )
); );
}; };
setupScript = pkgs.writeShellScript "setup-postgres" ''
set -euo pipefail
export PATH=${package}/bin:${pkgs.coreutils}/bin
POSTGRES_RUN_INITIAL_SCRIPT="false"
if [[ ! -d "${data_dir}" ]]; then
echo "Database directory does not exist. Initializing"
POSTGRES_RUN_INITIAL_SCRIPT="true"
${package}/bin/initdb -D ${data_dir} --no-instructions
fi
cp ${configFile} ${data_dir}/postgresql.conf
if [[ "$POSTGRES_RUN_INITIAL_SCRIPT" == "true" ]]; then
echo "Running initial script"
pg_ctl -D "${data_dir}" -w start -o "-c unix_socket_directories=${data_dir} -c listen_addresses= -p ${lib.toString port}"
pg_ctl -D "${data_dir}" -m fast -w stop
else
echo
echo "Database directory exists. Skipping initialization"
echo
fi
unset POSTGRES_RUN_INITIAL_SCRIPT
'';
script = pkgs.writeShellApplication { script = pkgs.writeShellApplication {
name = "run-postgres"; name = "run-postgres";
text = '' text = ''
if [ ! -d "${data_dir}" ]; then set -euo pipefail
echo "Database directory does not exist. Initializing" mkdir -p ${lib.escapeShellArg data_dir}
${package}/bin/initdb -D ${data_dir} --no-instructions ${setupScript}/bin/setup-postgres
fi
${package}/bin/postgres -D${data_dir} --config-file=${configFile} ${package}/bin/postgres -D${data_dir} --config-file=${configFile}
''; '';
}; };
in in