Forum OpenACS Q&A: Postgres not starting up after power failure

I've had a couple of power failures these last few days. Each time, my installation has failed to come back up.

I traced the problem down to the fact that when the server is shut off without any notice (ie not shut down properly), Postgres doesn't come up correctly, and so you get a page saying that the database isn't installed correctly.

I think the solution might be to check for the /usr/local/psql/data/postmaster.pid file on startup, and delete it if present.

This seems pretty important. Ideally, a server should be on battery backup. However, if this gives out, it should come back up ready to go.

Any comments on this? Any reason I shouldn't put this in a startup script? Where would be the best place to put it? The /etc/init.d scripts wouldn't be good, because you wouldn't want it to start up if was already started, right?

Collapse
Posted by Tom Jackson on

The startup script checks for the existance of the lock file (pid file). If it is there, the startup script doesn't start the process. The file probably still exists when the server goes down from a power failure.

Collapse
Posted by Jonathan Ellis on
I ran into this problem on my freebsd system, so I created a helper program which I call from the postgres init script:
#!/usr/local/bin/tclsh8.3

set PID_F /usr/local/pgsql/data/postmaster.pid

if [file exists $PID_F] {
    set pid [lindex [split [exec cat $PID_F] \n] 0]
    set exists [string length [exec ps ax | awk "\$1 == $pid {print}"]]

    if !$exists {
        exec rm $PID_F
        exec logger "pg_rmpid: invalid pid; removed $PID_F"
    }
}
Collapse
Posted by Jade Rubick on
Should this be put in the default install script? It seems like a good idea. A server shouldn't stay down if there was a power failure, right?

Do you call this at the beginning of the startup portion of your Postgres startup script?

Collapse
Posted by Jonathan Ellis on
yes, that is where I call it from
Collapse
Posted by Sreejith G on
I also got the similar problem.

I executed this  script.

su -l postgres -s /bin/sh -c "/usr/bin/initdb --pgdata=<PGDATA PATH>"

The PGDATA PATH I changed to some other path. The script  created all the necessary files there. I edited the /etc/init.d/postgres and changed the data directory path to the new path. I started the server using comand

/etc/init.d/postgres start

It started fine

Hope this helps

Regards
Sreejith
mailto:sreejith@marlabs.com
Marlabs