0

I am running a Flask app with SQLAlchemy, on Ubuntu server with PostgreSQL and Apache2.

SQLAlchemy gave an authentication failed for postgres error. After stopping Apache2, restarting Postgres and starting Apache2 again, it was resolved.

One day after that I had the same error. What is wrong in this setup?

PostgreSQL version:

PostgreSQL 12.18 (Ubuntu 12.18-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, 64-bit

Apache2 logs:

[Sun Jul 07 04:39:15.632091 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] , referer: http://example.example.com/
[Sun Jul 07 04:39:15.632100 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] , referer: http://example.example.com/
[Sun Jul 07 04:39:15.632104 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] The above exception was the direct cause of the following exception:, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632108 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] , referer: http://example.example.com/
[Sun Jul 07 04:39:15.632115 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] Traceback (most recent call last):, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632201 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/var/www/example2/html/example2/example.wsgi", line 3, in <module>, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632207 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     from example import app as application, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632214 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/var/www/example2/html/example2/example.py", line 11, in <module>, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632219 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     from models import (, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632226 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/var/www/example2/html/example2/models.py", line 1205, in <module>, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632230 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     create_tables(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632242 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/var/www/example2/html/example2/models.py", line 242, in create_tables, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632247 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     db.create_all(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632254 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/flask_sqlalchemy/__init__.py", line 1094, in create_all, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632258 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     self._execute_for_all_tables(app, bind, 'create_all'), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632265 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/flask_sqlalchemy/__init__.py", line 1086, in _execute_for_all_tables, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632270 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     op(bind=self.get_engine(app, bind), **extra), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632277 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/sql/schema.py", line 4744, in create_all, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632282 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     bind._run_ddl_visitor(, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632289 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 3007, in _run_ddl_visitor, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632293 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     with self.begin() as conn:, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632300 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 2923, in begin, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632304 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     conn = self.connect(close_with_result=close_with_result), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632311 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 3095, in connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632316 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return self._connection_cls(self, close_with_result=close_with_result), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632323 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 91, in __init__, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632327 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     else engine.raw_connection(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632334 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 3174, in raw_connection, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632339 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return self._wrap_pool_connect(self.pool.connect, _connection), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632346 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 3144, in _wrap_pool_connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632353 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     Connection._handle_dbapi_exception_noconnection(, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632361 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 2003, in _handle_dbapi_exception_noconnection, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632366 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     util.raise_(, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632373 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/compat.py", line 211, in raise_, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632377 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     raise exception, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632384 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py", line 3141, in _wrap_pool_connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632388 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return fn(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632395 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 301, in connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632400 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return _ConnectionFairy._checkout(self), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632406 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 755, in _checkout, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632411 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     fairy = _ConnectionRecord.checkout(pool), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632418 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 419, in checkout, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632422 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     rec = pool._do_get(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632429 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/impl.py", line 145, in _do_get, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632434 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     self._dec_overflow(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632440 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632445 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     compat.raise_(, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632452 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/compat.py", line 211, in raise_, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632456 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     raise exception, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632463 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/impl.py", line 142, in _do_get, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632470 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return self._create_connection(), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632478 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 247, in _create_connection, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632482 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return _ConnectionRecord(self), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632489 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 362, in __init__, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632501 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     self.__connect(first_connect_check=True), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632509 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 605, in __connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632514 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     pool.logger.debug("Error on connect(): %s", e), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632521 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632525 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     compat.raise_(, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632532 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/compat.py", line 211, in raise_, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632536 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     raise exception, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632543 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/pool/base.py", line 599, in __connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632548 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     connection = pool._invoke_creator(self), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632555 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/create.py", line 578, in connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632559 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return dialect.connect(*cargs, **cparams), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632566 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/default.py", line 558, in connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632571 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     return self.dbapi.connect(*cargs, **cparams), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632578 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]   File "/usr/local/lib/python3.8/dist-packages/psycopg2/__init__.py", line 122, in connect, referer: http://example.example.com/
[Sun Jul 07 04:39:15.632582 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268]     conn = _connect(dsn, connection_factory=connection_factory, **kwasync), referer: http://example.example.com/
[Sun Jul 07 04:39:15.632600 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  password authentication failed for user "postgres", referer: http://example.example.com/
[Sun Jul 07 04:39:15.632605 2024] [wsgi:error] [pid 40462:tid 140456050530048] [client 130.176.118.80:48268] FATAL:  password authentication failed for user "postgres", referer: http://example.example.com/
7
  • Can you send the logs?
    – szgezu
    Commented Jul 7 at 5:20
  • @szgezu added logs.
    – Yusif
    Commented Jul 7 at 5:51
  • And can you connect as dB user "postgres" with the settings/password in your flask config using psql? Commented Jul 7 at 6:00
  • @RichardHuxton I used '''psql -h localhost -p 5432 -U postgres db-name''' on the server, with the password in the config file and it failed. So if the problem is with the config, how does the error go away after restarting postgres?
    – Yusif
    Commented Jul 7 at 6:36
  • When you restart PostgreSQL do you reset the password as well? If not, that suggests the error message is incorrect and your connection is failing because of some other reason. Start by looking in the postgresql logs and maybe turning on connection/disconnection logging. Commented Jul 7 at 10:32

0