theory / pgenv

PostgreSQL binary manager
MIT License
322 stars 28 forks source link

Fix PGENV_SCRIPT_POSTINITDB script execution #70

Closed bkuhlmann closed 7 months ago

bkuhlmann commented 7 months ago

Why

Hello. :wave: I'm wanting to make use of the PGENV_SCRIPT_POSTINITDB environment variable so my custom script will run only once after initdb has finished but I never see the script execute.

How

Here's my setup:

# $HOME/.bashrc
export PGENV_SCRIPT_POSTINITDB="$HOME/.config/pgenv/setup"
# $HOME/.config/pgenv/setup
#! /usr/bin/env bash

set -o nounset
set -o errexit
set -o pipefail
IFS=$'\n\t'

psql --username postgres \
     --command "CREATE ROLE $USER WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD '';"
psql --username postgres --command "CREATE DATABASE $USER;"

The permissions of setup are 755 but when I use the following:

pgenv build 16.2
pgenv use 16.2

...the setup script never gets executed. I assume this is a bug or am I doing something wrong?

fluca1978 commented 7 months ago

This is a bug: the script is named PGENV_SCRIPT_POSTINITDB but the system tries to execute it with PGENV_SCRIPT_INITDB as in https://github.com/theory/pgenv/blob/master/bin/pgenv#L964

fluca1978 commented 7 months ago

@bkuhlmann please use v1.3.5, thanks for reporting.

bkuhlmann commented 7 months ago

Thanks! I picked up 1.3.5 and see the changes. The only problem is that it seems like PostgreSQL isn't fully running by the time my setup script is applied. Here's the output:

Running post-initdb script [/Users/bkuhlmann/.config/pgenv/setup]
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

Would you like me to report this as a different issue?

fluca1978 commented 7 months ago

Uhm... post-initdb scripts are meant to be run after initdb, at that time PostgreSQL is not supposed to be running. What you probably want is a post-start script, but you should handle the error on re-creating the same user (or checking before creating it).

@theory should we add a "run-once" post-start script to handle such kind of initialization? Something that is supposed to be run only once as the instance is started for the very first time?

theory commented 7 months ago

Sure why not?