Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Do not use single-user mode when initializing postgres #746

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions src/modules/services/postgres.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@ let
# Create initial databases
dbAlreadyExists="$(
echo "SELECT 1 as exists FROM pg_database WHERE datname = '${database.name}';" | \
postgres --single -E postgres | \
psql --dbname postgres | \
${pkgs.gnugrep}/bin/grep -c 'exists = "1"' || true
)"
echo $dbAlreadyExists
if [ 1 -ne "$dbAlreadyExists" ]; then
echo "Creating database: ${database.name}"
echo 'create database "${database.name}";' | postgres --single -E postgres

echo 'create database "${database.name}";' | psql --dbname postgres

${lib.optionalString (database.schema != null) ''
echo "Applying database schema on ${database.name}"
if [ -f "${database.schema}" ]
then
echo "Running file ${database.schema}"
${pkgs.gawk}/bin/awk 'NF' "${database.schema}" | postgres --single -j -E ${database.name}
${pkgs.gawk}/bin/awk 'NF' "${database.schema}" | psql --dbname ${database.name}
elif [ -d "${database.schema}" ]
then
# Read sql files in version order. Apply one file
# at a time to handle files where the last statement
# doesn't end in a ;.
ls -1v "${database.schema}"/*.sql | while read f ; do
echo "Applying sql file: $f"
${pkgs.gawk}/bin/awk 'NF' "$f" | postgres --single -j -E ${database.name}
${pkgs.gawk}/bin/awk 'NF' "$f" | psql --dbname ${database.name}
done
else
echo "ERROR: Could not determine how to apply schema with ${database.schema}"
Expand All @@ -57,12 +56,12 @@ let
cfg.initialDatabases)
else
lib.optionalString cfg.createDatabase ''
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | postgres --single -E postgres '';
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql --dbname postgres'';

runInitialScript =
if cfg.initialScript != null then
''
echo ${lib.escapeShellArg cfg.initialScript} | postgres --single -E postgres
echo ${lib.escapeShellArg cfg.initialScript} | psql --dbname postgres
''
else
"";
Expand All @@ -83,15 +82,49 @@ let
set -euo pipefail
export PATH=${postgresPkg}/bin:${pkgs.coreutils}/bin

SEED_DATABASES="false"
penguincoder marked this conversation as resolved.
Show resolved Hide resolved
if [[ ! -d "$PGDATA" ]]; then
initdb ${lib.concatStringsSep " " cfg.initdbArgs}
${setupInitialDatabases}

${runInitialScript}
SEED_DATABASES="true"
echo
echo "PostgreSQL init process complete; ready for start up."
penguincoder marked this conversation as resolved.
Show resolved Hide resolved
echo
fi

# Setup config
cp ${configFile} "$PGDATA/postgresql.conf"

if [[ "$SEED_DATABASES" = "true" ]]; then
echo
echo "PostgreSQL recently initialized; going to seed databases."
penguincoder marked this conversation as resolved.
Show resolved Hide resolved
echo
OLDPGHOST="$PGHOST"
PGHOST="$DEVENV_STATE/$(mktemp -d "pg-init-XXXXXX")"
mkdir -p "$PGHOST"

function remove_tmp_pg_init_sock_dir() {
if [[ -d "$1" ]]; then
echo
echo "Removing temporary socket directory for initialization/seeding: $1"
penguincoder marked this conversation as resolved.
Show resolved Hide resolved
echo
rm -rf "$1"
fi
}
trap "remove_tmp_pg_init_sock_dir '$PGHOST'" EXIT

pg_ctl -D "$PGDATA" -w start -o "-c unix_socket_directories=$PGHOST -c listen_addresses= -p ${toString cfg.port}"
${setupInitialDatabases}

${runInitialScript}
pg_ctl -D "$PGDATA" -m fast -w stop
remove_tmp_pg_init_sock_dir "$PGHOST"
PGHOST="$OLDPGHOST"
unset OLDPGHOST
else
echo
echo "PostgreSQL Database directory appears to contain a database; Skipping initialization"
echo
fi
'';
startScript = pkgs.writeShellScriptBin "start-postgres" ''
set -euo pipefail
Expand Down
Loading