PostgreSQL - Run server on startup (Linux)

Posted in Ubuntu by v.udartsev on 04 February 2020

1) Check your chmod file available:

sudo chmod +x /etc/rc.local

2) Edit rc.local file:

sudo gedit /etc/rc.local

3) Add:

# PostgreSQL
mkdir /var/run/postgresql
mkdir /var/run/postgresql/12-main.pg_stat_tmp
touch /var/run/postgresql/12-main.pg_stat_tmp/global.tmp
chown -R postgres:postgres /var/run/postgresql
su postgres -c '/usr/lib/postgresql/12/bin/pg_ctl start -D /etc/postgresql/12/main'

# OR CHANGE YOUR DB VERSION AND DATABASE FILES FOLDER
# su postgres -c '/usr/lib/postgresql/9.5/bin/pg_ctl start -D /etc/postgresql/9.5/main'

4) Enable rc.local instructions (it is turned off by default):

sudo systemctl enable rc-local.service

5) Start your rc.local scripts right now:

sudo systemctl start  rc-local.service