0

I'm working on deploying my flask application on nginx and gunicorn and for a while I've been able to access the site on my mobile device but a couple of days ago it abruptly stopped. The logs showed that postgres was not starting. When I checked the postgres logfile I got the following error.

2022-08-03 15:23:42.681 EAT [32977] LOG:  starting PostgreSQL 14.2 on x86_64-pc-
linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
2022-08-03 15:23:42.682 EAT [32977] LOG:  listening on IPv4 address "127.0.0.1", port 
5432
2022-08-03 15:23:42.824 EAT [32977] LOG:  listening on Unix socket 
"/tmp/.s.PGSQL.5432"
2022-08-03 15:23:42.891 EAT [32978] LOG:  database system was shut down at 2022-08-03 
15:22:21 EAT
2022-08-03 15:23:42.926 EAT [32977] LOG:  database system is ready to accept 
connections
2022-08-03 16:15:01.391 EAT [32977] LOG:  received fast shutdown request
2022-08-03 16:15:01.448 EAT [32977] LOG:  aborting any active transactions
2022-08-03 16:15:01.449 EAT [32977] LOG:  background worker "logical replication 
launcher" (PID 32984) exited with exit code 1
2022-08-03 16:15:01.449 EAT [32979] LOG:  shutting down
2022-08-03 16:15:01.609 EAT [32977] LOG:  database system is shut down

It's not clear to me where the kill signal is coming from for when I run netstat -pln |grep 5432 this is the result returned

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      
17301/postgres      
unix  2      [ ACC ]     STREAM     LISTENING     1258541  17301/postgres       
/tmp/.s.PGSQL.5432

and its clear to me that the database is running or so I think.

I have configured Nginx and Gunicorn to start on boot using Supervisord and I'm looking to do the same for Postgresql.

I believe the error has something to do with the application not waiting for the database to start or if it does then it restarts the database as it should but I can't be too sure about it.

Any help with be highly appreciated.

6
  • What's your question?
    – mustaccio
    Commented Oct 8, 2022 at 20:30
  • If you haven't configured it to start upon boot yet, then how is it getting started?
    – jjanes
    Commented Oct 9, 2022 at 1:46
  • My question @mustaccio is how to prevent this from occuring, thank you Commented Oct 9, 2022 at 8:06
  • @jjanes I'm starting it manually with `/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start'. Thank you. Commented Oct 9, 2022 at 8:08
  • 1
    Does the shutdown correspond to when you close the terminal window you used to execute pg_ctl, or when you log out, or something like that? I think I would start by making it start on boot, and seeing if that solves the problem. Since it is something you want to do anyway.
    – jjanes
    Commented Oct 9, 2022 at 20:47

1 Answer 1

0

The solution that worked for me is configuring postgres to start on boot along with the application. According to a source I found, the possible cause of the behavior observed initially is the fact that supervisor starts every entity of an app and in case the database is running, then it will shut it down and attempt to restart it again so that's why I needed to configure it to start on boot. To do this add this to your supervisor configuration file below the configuration code for your application.

[program:postgresql]
user=postgres
command=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start              
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
redirect_stderr=true

The description is not a typical issue but a highly accurate suggestion in my case. I hope this helps someone. Cheers!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.