Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if a directory is a valid PostgreSQL cluster

Tags:

postgresql

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?

like image 335
James N Avatar asked Aug 14 '15 15:08

James N


2 Answers

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.

like image 82
Soping Lee Avatar answered Oct 17 '22 09:10

Soping Lee


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

like image 45
Rajeev Rathor Avatar answered Oct 17 '22 08:10

Rajeev Rathor