22

I've installed postgresql by pacman. Now I'm trying to run it:

    $ sudo systemctl start postgresql
    Job for postgresql.service failed because the control process exited with error code.
    See "systemctl status postgresql.service" and "journalctl -xe" for details.

And then:

    $ sudo systemctl status postgresql
    ● postgresql.service - PostgreSQL database server
       Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Sun 2016-07-10 15:30:47 UTC; 17s ago
      Process: 19468 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=1/FAILURE)

    Jul 10 15:30:47 my_comp systemd[1]: Starting PostgreSQL database server...
    Jul 10 15:30:47 my_comp systemd[1]: postgresql.service: Control process exited, code=exited status=1
    Jul 10 15:30:47 my_comp systemd[1]: Failed to start PostgreSQL database server.
    Jul 10 15:30:47 my_comp systemd[1]: postgresql.service: Unit entered failed state.
    Jul 10 15:30:47 my_comp systemd[1]: postgresql.service: Failed with result 'exit-code'.

What's wrong with it?

Matudi
  • 403
  • 1
  • 5
  • 6

4 Answers4

23

Following step solved your problem

step 1: create the data directory (acordingly with the PGROOT variable set before in the config file)

sudo mkdir /var/lib/postgres/data

Step 2: set /var/lib/postgres/data ownership to user 'postgres'

chown postgres /var/lib/postgres/data

Step 3: As user 'postgres' start the database.

sudo -i -u postgres
initdb  -D '/var/lib/postgres/data'

Step 4: Start the service as root

Rakib
  • 2,347
  • 1
  • 17
  • 20
  • 2
    Depending on version, this may need to be `initdb -D '/var/lib/pgsql/data'` – Wedge Martin Aug 25 '17 at 16:44
  • If you had a few databases, this will create fresh Postgresql server without old databases! – Chalist Dec 13 '19 at 03:40
  • step4: how to start the service in root? – Akash Pagar Jun 05 '20 at 07:35
  • yes. it worked. by logged in using ```sudo -i -u postgres``` start service using ```pg_ctl -D /var/lib/postgres/data -l logfile start``` – Akash Pagar Jun 05 '20 at 13:00
  • This does start the database successfully, but when I run `systemctl status postgresql.service`, I still get the same error as provided by OP. What is happening here? – Jason Goal Jan 21 '23 at 02:05
  • Fedora, for Fedora 34+ (where XX is the version of PG 14 or15 and possibly higher) with PG is "/var/lib/pgsql/XX/data" or "/var/lib/pgsql/data" for 13 and probably lower Also, -D will make a new database, so be careful if you already have one... there will be a prompt if you do, but still ;) – rhymsy Jun 29 '23 at 16:59
5

run these commands as said on /usr/share/doc/postgresql/README.rpm-dist as root user

postgresql-setup --initdb
systemctl restart postgresql.service
systemctl enable postgresql.service
GAD3R
  • 63,407
  • 31
  • 131
  • 192
0

The logging you shared includes this line:

/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=1/FAILURE)

What is $PGROOT set to? Does $PGROOT/data exist? Does this directory have any files init? If not, you may need to run initdb on the directory.

To initialize, create an empty data directory as root, then use chown to assign ownership of that directory to the database user account, then su to become the database user to run initdb /path/to/data.

Mark Stosberg
  • 7,420
  • 1
  • 32
  • 42
0

After setting a password for the postgres user:

$ sudo passwd postgres 

run the following command:

$ su - postgres -c "initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'"
cass
  • 101
  • 2