A
A
aminodovborisov2021-09-21 07:53:53
PostgreSQL
aminodovborisov, 2021-09-21 07:53:53

How to make PostgreSQL work with Telegram bot?

Hello!
There is a Telegram bot. The bot sends all statistics of user actions to the PostgreSQL database. The database is deployed on a remote server, on the same server as the bot.
But, for example, this morning I tried to connect to the database. And even on the simplest :~$ psql -hlocalhost command I got an error:

psql: error: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?


It turns out that the base spontaneously stops at some point. And in the logs I saw Shutting Down.
After I do sudo service postgresql restart the database works.
And if the base stops, then it is obvious that various requests to it from the telegram bot are wasted. Of course, I enclosed queries to the database in try ... except, so users do not notice anything. Most likely, the bot is trying to make a request to the database, sees that the database is disabled, issues an exception and continues to work.
Actually the question is:
How to ensure that
1) Either the database works continuously and does not fall off spontaneously,
2) Or the bot itself, seeing that the database is stopped, starts it?
Maybe it's realistic to make an automatic sudo service postgresql restart so that it runs on its own after some period of time?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2021-09-21
@ky0

Postgres doesn't stop just like that, you need to diagnose it. What's in the logs?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question