This works (i.e. the database is started successfully) but I never(?) get the prompt back (after entering the directory with devenv.nix, using direnv).
If I add pg_ctl stop after the last line (echo ...), everything works as expected and I do get the prompt back.
It appears that “something” (direnv maybe?) notices the new process and continues to wait for it?
That clashes with my PGDATA. I appreciate that there is a default way to start Postgres but I want/need more control.
Given that services.postgres.enable exists, what is done differently there? I tried using exec (that I found there) but I still get the same problem: no prompt.
Hmm. I’d try adding a set -x to your enterShell script. If everything is running in the same shell process it might help clarify what’s going on?
Modern pg_ctl start should wait until the server starts before returning, so I guess it could be failing to do so, but that doesn’t square w/ your note about adding a pg_ctl stop fixing the behavior.
Are you certain it’s stable behavior? Do you see pg processes from outside of the environment? I don’t see an attempt to stop/kill the server on exit, so you might be seeing ~stateful behavior from already-running processes leftover from previous attempts?
The script finishes. The echo is executed after all. And yes, I can see in a separate terminal that everything is running as expected. It seems to be just about what happens afterenterShell finishes.
It is totally repeatable (at least by me). I wanted to keep the script as simple as possible but I stop PG and remove the PGDATA directory manually.
(The echo 'Postgres started' is the last line of my enterShell.)
If I add pg_ctl stop after that final echo, the output is identical except for the additional line output by direnv about the exported variables. And I get my prompt back.
The “original” terminal simply hangs. I can’t ctrl-c it but I can close the terminal tab and it will die (it no longer shows up in ps -ef). Other terminals show that all expected effects/actions have taken place.
The issue you linked to seems to be exactly what I am running into. And indeed, your suggestion about killing PG in another terminal does result in the “original” terminal to finish and I get back my prompt.
Ah, but I can use my earlier trick. I first start PG, create the database, and then stop PG. After that, I use the background trick. It’s redundant but the database creation only happens once (or rarely).