EDIT: the original question can be found below. Basically, this is a misconception on my part of how
pg_ctl works and if you're getting this message too (from
pg_ctl), then the TLDR; is, as @jjanes explains in the accepted answer:
pg_ctl doesn't track your database. It interacts with a specific directory, either specified by -D, or through the PGDATA env variable.
postgres you're connected to must have been started somehow, maybe not by you, but was given a
PGDATA directory to work with.
pg_ctl is not your interface to multiple running
I created a directory in
postgres user's home dir called
I did this using
initdb -D mydb.
Now, I want to start
postgres and use
mydb as the location where all this db's data is stored; so I run:
pg_ctl -D /var/lib/postgresql/mydb start
It starts successfully, but if I try:
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1)) Type "help" for help. postgres=# show data_directory; data_directory ----------------------------- /var/lib/postgresql/12/main (1 row)
i.e. it's using the "main" or default data directory, completely ignoring the
So now it occurs to me that I might be connecting to a different db... even though running
pg_ctl status before
pg_ctl start gives me:
pg_ctl: no server running
However, this has to be the case because... with "no server running", I am able to
psql and query the main database.
So then, how do I get the main database "tracked" by
pg_ctl? Also, how do I use
psql to connect to the right database?
Edit: was able to connect to the postgres started with
psql -h /var/run/postgresql
(even though I don't see any changes to that directory's contents before / after I
pg_ctl start i.e. it remains:
Edit: Also I can see that the postgres process not being tracked by
pg_ctl was started using
postgres executable (from running
postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf
Probably from a startup file. I would imagine I need to find this startup file and replace use of