I have some code which calls the pg_ctl command on a given path and checks the output to determine whether it is a valid PostgreSQL cluster:pg_ctl status -D <data_directory>

However, while unittesting a negative on a known non-cluster directory, it isn't reliably returning the expected result of pg_ctl: directory <data_directory> is not a database cluster directory on my build server; instead it returns pg_ctl: no server running.

Is there a more fool-proof way of determining if the directory is a valid cluster?

2

Best Answer


Use the following commands to initialize the DB directory.

$ initdb -D <data_directory>

Then you can get the information about initialization.

By the way, if you want to start your psql quickly, set PGDATA variable in configuration file, then use pg_ctl start to start your psql.

To start first time your Postgres server with single node. We should following steps,

Step 1. Create directory let say: /home/rajeev/pg_data_1

Step 2. initialize the cluster that copy all exe, lib, dependency config etc.postgres_home> initdb -D /home/rajeev/pg_data_1

if successfull initialize your server you will get following log message

Success. You can now start the database server using:pg_ctl -D ^"/home/rajeev/pg_data_1" -l logfile start

Step 3: Start Postgres node $postgres_home>pg_ctl.exe -D /home/rajeev/pg_data_1 start -U postgres -P pass123 -S auto

Step 4: start postgress CLI

psql -U postgres