Postgresql does not run at boot

Hi,

I have an issue with postgresql service, it does not start at boot even though it is enabled on Startup.

image

I have to click Start button to start manually.

The temporary solution now is to add command in the Local Startup so it will start at boot.

The content of script is as below:

#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2015 OpenWrt.org
START=93

PROG=/usr/bin/postmaster

USE_PROCD=1

fix_hosts() {
	# make sure localhost (without a dot) is in /etc/hosts
	grep -q 'localhost$' /etc/hosts || echo '127.0.0.1 localhost' >> /etc/hosts
}

fix_perms() {
	# for whatever reason, /dev/null gets wrong perms
	chmod a+w /dev/null
}

cleanup() {
	if [ -f "$1/postmaster.pid" ]; then
		rm "$1/postmaster.pid"
	fi
}

start_service() {
	. /lib/functions/postgresql.sh

	config_load "postgresql"
	config_get pgdata config PGDATA
	config_get pgopts config PGOPTS

	user_exists postgres 5432 || user_add postgres 5432
	group_exists postgres 5432 || group_add postgres 5432

	[ "$_BOOT" = "1" ] &&
		[ "$(procd_get_mountpoints $pgdata)" ] && return 0

	fix_perms
	fix_hosts

	if [ ! -e "${pgdata}/PG_VERSION" ]; then
		pg_init_data ${pgdata}
		[ $? -gt 0 ] && return 1
	fi

	cleanup "${pgdata}"

	mkdir -m 0755 -p /var/run/postgresql
	chmod 0750 /var/run/postgresql
	chown postgres:postgres /var/run/postgresql
	procd_open_instance postmaster
	procd_set_param user postgres
	procd_set_param command $PROG
	procd_append_param command -D "${pgdata}"
	procd_append_param command -k "/var/run/postgresql"
	[ -n "${pgopts}" ] && procd_append_param command -o "${pgopts}"
	procd_set_param respawn retry=60
	procd_add_jail postgresql log
	procd_add_jail_mount /usr/lib/postgresql /usr/share/postgresql
	procd_add_jail_mount_rw /var/run/postgresql "${pgdata}"
	procd_add_jail_mount_rw /dev/shm
	procd_set_param stderr 1
	procd_set_param stdout 1
	procd_close_instance

	procd_open_instance uci_dbinit
	procd_set_param user postgres
	procd_set_param command /lib/functions/postgresql.sh init "${pgdata}"
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

boot() {
	_BOOT=1 start
}

service_triggers() {
	config_load "postgresql"
	config_get pgdata config PGDATA
	procd_add_restart_mount_trigger "${pgdata}"
}

stop_service() {
	procd_send_signal "postgresql" postmaster SIGTERM
}

status_service() {
	config_load "postgresql"
	config_get pgdata config PGDATA
	/usr/bin/pg_ctl status -U postgres -D "${pgdata}"
}

Anyone have idea why it could not start at boot?

While looking through your scripts, there are 2 things which strike me.

  • in rc.local, you have a line mkdir /var/run/http. Is that for postgresql? If that line is necessary, it’s not strange the service won’t start.
  • You start the service with ‘start’. The script contains a function boot(), so I assume that is used when starting the script at boot. The difference between ‘start’ and ‘boot’ is in the line [ "$_BOOT" = "1" ] &&
    [ "$(procd_get_mountpoints $pgdata)" ] && return 0
    It’s hard to say what happens here, except that it is possible the lines below are not executed, and it has something to do with mountpoints. Is it possible that your database is on a filesystem which is not yet mounted?

Hi,

mkdir /var/run/http has nothing to do with postgresql, it is for web service.

I changed the startup sequence for postgresql from original 50 to 93 and also tried 99, nothing happened. I am pretty sure the mount point is ok because another script is using the same partition and it started ok at boot.

Any code can be added to the postgresql script so it can be seen in the log?

Try this: service postgresql start

Yes. Just logger text

Starting manually by command has no issue, it just won’t auto start at boot

Hi,

I added text logger “postgresql boot” into boot section

boot() {
    logger "postgresql boot"
	_BOOT=1 start
}

The system log shows “postgresql boot”, so the script has been executed, something wrong with the code “_BOOT=1 start”.

I think that if you put logline before and after

[ "$_BOOT" = "1" ] &&
[ "$(procd_get_mountpoints $pgdata)" ] && return 0

you’ll see it exits there.

I put “logger postgresql 1” before and “logger postgresql 2” after, i can see “postgresql 1” in the log, “postgresql 2” is not in the log, postgresql does not start.

then I move “start” above “_BOOT=1”:

boot() {
    logger "postgresql boot"
	start
    _BOOT=1 
}

It works. I can see both “postgresql 1” and “postgresql 2” in the log. Not sure why.

‘start’ somehow calls start_service(). When ‘_BOOT=1’ is set before that, it will bail out on [ "$(procd_get_mountpoints $pgdata)" ] && return 0 . When it’s not set, this code isn’t run.

So maybe you should put a logger "$(procd_get_mountpoints $pgdata)" before that, to see why it evalutates to true.

I completely remove and re-install postgresql, with all default settings, postgresql starts every time when router reboots. I took a deep look at the configuration file /etc/config/postgresql:

config postgresql config
	option PGDATA	/var/postgresql/data

However, directory /var in openwrt is actually /tmp, it will be deleted on every reboot. When executing /etc/init.d/postgresql at boot, “$(procd_get_mountpoints $pgdata)” does not find /var/postgresql/data, then it executes the following lines to create new database. In my configuration, pgdata is in /opt/postgresql/data which persists after rebooting, then it will execute “return 0” without start the service.

I add “service postgresql start: to Local StartUp, it is working on evert reboot.

Which OpenWrt version you are running?
There was recent change to that part of the startup script (in master and 25.12, but not in 24.10 or earlier)

If you are running 24.10, you might test applying the change from that commit.

Thank you for the information. I edited /etc/init.d/postgresql and postgresql started normally at boot.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.