postgresql installation : initdb data directory not empty?

sridhar asked:

I am trying to create the postgresql database.

When I install PostgreSQL, I gave this command:

sudo yum install postgresql postgresql-server

and later I modified the config file:

sudo vim /var/lib/pgsql/data/pg_hba.conf

and modified as

local   all         all                               trust
host    all         all          trust
host    all         all         ::1/128               trust
host    all         all             md5

While I am trying to startup the postgresql service:

sudo service postgresql initdb

> Data directory is not empty!                               [FAILED]

sudo chkconfig postgresql on

sudo service postgresql start

Starting postgresql service:                               [  OK  ]

What is caused these errors and how do I fix them?

My answer:

On systemd based systems like RHEL/CentOS 7 and Fedora the procedure for running initdb is somewhat different. This is no longer done by the init scripts (which no longer exist), and the new procedure is much closer to the upstream instructions.

You must first su to the postgres user, and then run initdb or pg_ctl initdb. It’s not necessary to provide a data directory if you are using a Red Hat build as its default automatically chooses the default data directory /var/lib/pgsql.

For example:

# su - postgres
$ pg_ctl initdb
$ exit

Of course, you only do this once, on first installation, to set up the initial data directory. You would not do it again unless you were creating a completely new installation or restoring from a disaster.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.