From f8cd3fbcb5c31456746b135b8eacb373499a44ae Mon Sep 17 00:00:00 2001 From: Adrian Date: Wed, 21 Feb 2024 19:06:56 +0100 Subject: [PATCH] postgres init: use -U ${config.superuser} with psql if superuser != null (#106) --- nix/postgres/setup-script.nix | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nix/postgres/setup-script.nix b/nix/postgres/setup-script.nix index e1a4e8d9..59c7a1a2 100644 --- a/nix/postgres/setup-script.nix +++ b/nix/postgres/setup-script.nix @@ -1,11 +1,12 @@ { config, pkgs, lib }: let + psqlUserArg = lib.optionalString (config.superuser != null) "-U ${config.superuser}"; setupInitialSchema = dbName: schema: '' echo "Applying database schema on ${dbName}" if [ -f "${schema}" ] then echo "Running file ${schema}" - awk 'NF' "${schema}" | psql -d ${dbName} + awk 'NF' "${schema}" | psql ${psqlUserArg} -d ${dbName} elif [ -d "${schema}" ] then # Read sql files in version order. Apply one file @@ -13,7 +14,7 @@ let # doesn't end in a ;. find "${schema}"/*.sql | while read -r f ; do echo "Applying sql file: $f" - awk 'NF' "$f" | psql -d ${dbName} + awk 'NF' "$f" | psql ${psqlUserArg} -d ${dbName} done else echo "ERROR: Could not determine how to apply schema with ${schema}" @@ -28,13 +29,13 @@ let # Create initial databases dbAlreadyExists=$( echo "SELECT 1 as exists FROM pg_database WHERE datname = '${database.name}';" | \ - psql -d postgres | \ + psql ${psqlUserArg} -d postgres | \ grep -c 'exists = "1"' || true ) echo "$dbAlreadyExists" if [ 1 -ne "$dbAlreadyExists" ]; then echo "Creating database: ${database.name}" - echo 'create database "${database.name}";' | psql -d postgres + echo 'create database "${database.name}";' | psql ${psqlUserArg} -d postgres ${lib.optionalString (database.schemas != null) (lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas)} fi @@ -42,13 +43,13 @@ let config.initialDatabases) else lib.optionalString config.createDatabase '' - echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql -d postgres ''; + echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql ${psqlUserArg} -d postgres ''; runInitialScript = let scriptCmd = sqlScript: '' - echo "${sqlScript}" | psql -d postgres + echo "${sqlScript}" | psql ${psqlUserArg} -d postgres ''; in {