How to make Postgres start automatically on boot

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

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

4 Answers

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

From Ubuntu 15.04 onwards do:

sudo systemctl enable [SERVICE]

Which in your case is:

sudo systemctl enable postgresql
2

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like