From 18d5c1d168ada2dbc179b9009780dfcc4ee38d2b Mon Sep 17 00:00:00 2001 From: Shivaraj B H Date: Tue, 26 Mar 2024 18:44:11 +0530 Subject: [PATCH] fix(postgres): empty `socketDir` by default (#160) resolves #139 > empty `unix_socket_directories` means not listening on any Unix-domain sockets, in which case only TCP/IP sockets can be used to connect to the server. see: https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORIES * use TCP/IP by default for health check --- doc/postgresql.md | 3 +++ nix/postgres/default.nix | 18 +++++++++++++----- nix/postgres/setup-script.nix | 19 +++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/doc/postgresql.md b/doc/postgresql.md index 7629f8c..67aefdd 100644 --- a/doc/postgresql.md +++ b/doc/postgresql.md @@ -20,6 +20,9 @@ {#socket-path} ### Unix-domain socket path is too long +> [!warning] +> Only relevant if `socketDir` is set. If not, postgres uses TCP/IP by default. + We already talk about this in the [data directory guide](datadir.md#socket-path). In case of postgres, you can set `socketDir` while keeping the `dataDir` unchanged. >[!note] diff --git a/nix/postgres/default.nix b/nix/postgres/default.nix index 1f5dbec..8611e03 100644 --- a/nix/postgres/default.nix +++ b/nix/postgres/default.nix @@ -52,8 +52,12 @@ in socketDir = lib.mkOption { type = lib.types.str; - default = config.dataDir; + default = ""; description = "The DB socket directory"; + defaultText = '' + An empty value specifies not listening on any Unix-domain sockets, in which case only TCP/IP sockets can be used to connect to the server. + See: https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORIES + ''; }; # Based on: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS @@ -128,7 +132,7 @@ in listen_addresses = lib.mkOption { type = lib.types.str; description = "Listen address"; - default = ""; + default = "127.0.0.1"; example = "127.0.0.1"; }; @@ -309,13 +313,17 @@ in text = '' set -euo pipefail PGDATA=$(readlink -f "${config.dataDir}") - PGSOCKETDIR=$(readlink -f "${config.socketDir}") export PGDATA - postgres -k "$PGSOCKETDIR" + ${ if config.socketDir != "" then '' + PGSOCKETDIR=$(readlink -f "${config.socketDir}") + postgres -k "$PGSOCKETDIR" + '' else '' + postgres + ''} ''; }; pg_isreadyArgs = [ - "-h $(readlink -f \"${config.socketDir}\")" + "-h ${config.listen_addresses}" "-p ${toString config.port}" "-d template1" ] ++ (lib.optional (config.superuser != null) "-U ${config.superuser}"); diff --git a/nix/postgres/setup-script.nix b/nix/postgres/setup-script.nix index 59c7a1a..69dccb7 100644 --- a/nix/postgres/setup-script.nix +++ b/nix/postgres/setup-script.nix @@ -97,17 +97,24 @@ in # Setup config echo "Setting up postgresql.conf" cp ${configFile} "$PGDATA/postgresql.conf" - # Create socketDir if it doesn't exist - if [ ! -d "${config.socketDir}" ]; then - echo "Creating socket directory" - mkdir -p "${config.socketDir}" - fi + # Create socketDir if it doesn't exist and it is not empty + ${lib.optionalString (config.socketDir != "") '' + if [ ! -d "${config.socketDir}" ]; then + echo "Creating socket directory" + mkdir -p "${config.socketDir}" + fi + ''} if [[ "$POSTGRES_RUN_INITIAL_SCRIPT" = "true" ]]; then echo echo "PostgreSQL is setting up the initial database." echo - PGHOST=$(mktemp -d "$(readlink -f "${config.socketDir}")/pg-init-XXXXXX") + ${ if config.socketDir != "" then '' + PGHOST=$(mktemp -d "$(readlink -f "${config.socketDir}")/pg-init-XXXXXX") + '' else '' + PGHOST=$(mktemp -d /tmp/pg-init-XXXXXX) + '' + } export PGHOST function remove_tmp_pg_init_sock_dir() {