34

I am new to Ubuntu and I would like to know how to make postgresql start automatically on boot and how to configure my setup so that I can start the postgres server if need be.

I am working on a small rails project and I have been having difficulties with postgres starting correctly. I have had to resort to uninstalling and reinstalling postgres to just get the project up and running just to get work done. I have been using these commands.

sudo apt-get -y update
sudo apt-get purge postgresql* # ending * is important
sudo apt-get install postgresql libpq-dev

from this post

https://stackoverflow.com/questions/17934055/postgresql-cannot-connect-to-server-locally

skipping

sudo add-apt-repository ppa:pitti/postgresql

as it appears it is out of date and there's a comment below about not needing it.

If it helps my pg_hba.conf file appears to be located at

/etc/postgresql/9.1/main

I am running Ubuntu 12.04 on top of chrome os

Thanks all for the help and sorry for the newb question!

4
  • Why are you using some random person's PPI? Use apt.postgresql.org if you want current PostgreSQL releases on older platforms. Oct 20, 2014 at 7:43
  • Craig thanks for the response. Just to clarify the what would the command be to pull from the apt.postgresql.org be?
    – cwmacken
    Oct 22, 2014 at 4:05
  • @cwmacken there are instructions in the linked page (including commands).
    – muru
    Oct 22, 2014 at 4:18
  • @Craig haha sorry missed that it was a link. Thanks for the help! Cheers
    – cwmacken
    Oct 22, 2014 at 4:20

4 Answers 4

51

If you want to start postgres on startup so that you wont have to restart is all the time simply do:

sudo update-rc.d postgresql enable

this will always start your postgres on boot startup. hope it helps someone

3
  • I get error: update-rc.d: error: cannot find a LSB script for postgrsql But I installed postgresql from other repo (1c.postgrespro.ru/deb) because it has special patches I need. Sep 22, 2016 at 16:10
  • Thank you! My problem was: I disabled the service with systemctl disable postgresql.service. But I was no longer able to enable it with systemctl. Your answer was the solution. My system: Debian 8.8 x64 with PostgreSQL 9.4.
    – mfreiholz
    Dec 13, 2017 at 6:51
  • According to the postgresql manual it should run by the postgres user. Does this command start the postgresql service using the "postgres" user?
    – Readren
    May 2, 2021 at 0:14
28

From Ubuntu 15.04 onwards do:

sudo systemctl enable [SERVICE]

Which in your case is:

sudo systemctl enable postgresql
2
  • it works, however; after starting ubuntu a popup asks for admin password, anyway to get rid of it? Mar 16, 2017 at 12:03
  • Sorry, I've been only using a headless ubuntu on a server-system, so I never encountered that issue :-( Maybe someone else ran into the same problem and can help.
    – Kim
    Mar 16, 2017 at 13:20
4

So I figured out how to boot postgresql so i dont need to do the reinstall newb move.

sudo service postgresql start

you will then need to switch to the postgres user to do any changes within Postgresql

sudo -u postgres -i

I'm sure there are much better answers to this question than mine but this might help someone in my position in the future.

I still need to make Postgresql start on boot. Anyone got the answer to that? Ill gladly mark it as correct.

Cheers

1
  • There is an answer to start automatically upon boot
    – Anwar
    Apr 28, 2016 at 14:13
3

In the case you don't use the default configuration, like a particular cluster folder, or a custom port, an alternative is to create a crontab task that triggers on startup; and configure that task to execute a script that starts the postgresql service or whatever you like.

Step 1: switch to the postgres user

sudo su - postgres

Step 2: Use an editor to create the script named "start.sh" in the home folder of the postgres user, and fill it with the code that starts the postgresql service (or whatever you like). In my case (debian 10, and postgresql 11) the cluster folder ("main") is in a mounted disk ("/mnt/disk"), so the scrip is:

#!/bin/sh
/usr/lib/postgresql/11/bin/pg_ctl -D /mnt/disk-b/main -l /var/log/postgresql/pg_ctl.log start

Step 3: make said script be executable

chmod +x $HOME/start.sh

Step 4: create the crontab task that triggers on reboots.

crontab -e

which opens an editor containing the tasks schedule associated to the postgres user.

Step 5: Use the opened editor to insert a single line with this content

@reboot $HOME/start.sh

Step 6: verify the task was added

crontab -l

Note that the start script is executed using the postgres user, which is a requirement according to the postgresql manual.

Edit 1: If your postgresql installation package includes the command pg_createcluster, a better alternative is to use said command to create the cluster, and the systemctl command to start the "postgresql" service. This way the service is automatically configured to start at boot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .