25

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 127.0.0.1/32 trust host all all ::1/128 trust host all all 0.0.0.0/0 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?

4 Answers 4

20

Initdb should only be run once. It will create the directory where you'll keep the config files and (usually) the actual database. You've obviously already done that already; otherwrise there wouldn't be any pg_hba.conf for you to edit.

So, just don't run postgresql initdb again, unless you are doing a complete reinstall.

3
  • thanks for the reply.. i run it twice... you mean the its not the problem in installation.. Commented May 8, 2013 at 8:36
  • I mean that there is no problem with your installation, at least not any that you've shown. The only thing that is failing is doing initdb more than once, and that should fail - everything else shows as OK. If there's any other problem, it would help if you could describe it. Commented May 8, 2013 at 8:39
  • Thank you very much.. then its not a problem.. if i face a problem i will let you know.. Commented May 8, 2013 at 8:47
19

From here:

If you're completely wiping & reinstalling a Postgres DB, when running initdb like:

service postgresql-9.2 initdb -E 'UTF8' --pgdata="/foo/bar/"

you can encounter this service error:

Data directory is not empty! [FAILED]

To fix it (and this is the nuclear option -- all db data is wiped!)

On Amazon Linux (2014-x):

rm -rf /var/lib/pgsql9/data

On CentOS (6.x)

rm -rf /var/lib/pgsql/9.2/data

Now try the initdb command again and it should work this time:

service postgresql-9.2 initdb

0
5

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.

1
  • Hi, "you only do this once", did you mean run pg_ctl initdb only once whatsoever the options are followed, or did you mean that one can run both pg_ctl initdb and pg_ctl initdb -D /some/custom/dir as long as the command point to a new data_directory? Commented Jan 20, 2023 at 12:06
4

I had the same issue, using PostgreSQL 9.3 on CentOS 6.

I deleted the /var/lib/pgsql/9.3/data folder, then re-ran the command

sudo service postgresql-9.3 initdb 

... which successfully initialised the db service again.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.