(psycopg2.OperationalError) could not connect to server: Connection refused Is the server

Question:

I am trying to connect two docker containers, one posgresql and the other a python flask application. both are linking correctly, all connection variables in the python app are taken directly from the ones in the postgres container that are exposed via linking and are identical to those found when inspecting the postgresql container. when I use psql with the exact parameters from the connection string i.e:

psql -p 5432 -h 172.17.0.2 -d mydb -U user

connection to the database in the postgres container is successful, so I know that postgres is communicating correctly over the specified ports etc. however, when I attempt to connect to the database via flask with the same connection variables I get this error:

Traceback (most recent call last):   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1122, in _do_get
    return self._pool.get(wait, self._timeout)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py", line 145, in get
    raise Empty sqlalchemy.util.queue.Empty

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2138, in _wrap_pool_connect
    return fn()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
    return _ConnectionFairy._checkout(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)   File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async) psycopg2.OperationalError: could not connect to server: Connection refused     Is the server running on host "172.17.0.2" and accepting    TCP/IP connections on port 5432?


 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):   File "./wsgi.py", line 14, in <module>
    app = getModule("/var/www", name).app   File "./wsgi.py", line 5, in getModule
    return imp.load_source(module, ("%s/%s.py" % (path, name)))   File "/usr/local/lib/python3.6/imp.py", line 172, in load_source
    module = _load(spec)   File "/var/www/backend.py", line 3, in <module>
    import json, logging, db, os   File "/var/www/db.py", line 36, in <module>
    engine.connect()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2082, in connect
    return self._connection_cls(self, **kwargs)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 90, in __init__
    if connection is not None else engine.raw_connection()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2168, in raw_connection
    self.pool.unique_connection, _connection)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2142, in _wrap_pool_connect
    e, dialect, self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1456, in _handle_dbapi_exception_noconnection
    exc_info   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2138, in _wrap_pool_connect
    return fn()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
    return _ConnectionFairy._checkout(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
    self._dec_overflow()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
    return self._create_connection()   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)   File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async) sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused   Is the server running on host "172.17.0.2" and accepting    TCP/IP connections on port 5432?

below is the code in my flask app leading up to the exception:

import os, logging, json
from datetime import datetime
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import BigInteger, Integer, String, DateTime, Column, Table, ForeignKey, create_engine
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base

user = os.environ.get('DB_USER','user')
password = os.environ.get('DB_PASSWORD','password')
address = os.environ.get('DB_ADDR','0.0.0.0')
port = os.environ.get('DB_PORT','')
name = os.environ.get('DB_NAME','')

db_url = "postgresql://%s:%s@%s:%s/%s" % (user, password, address, port, name) 

engine = create_engine(db_url, convert_unicode=True, client_encoding='utf8')

print (engine)

engine.connect()

when I log the "engine" I get this:

Engine(postgresql://user:***@172.17.0.2:5432/mydb)

which again matches the parameters used in succesful database connection with the database from my host computer.

I’m assuming there must be something wrong with my python code since the docker containers appear by all accounts to be working correctly.

any help would be much appreciated, thank you.

Asked By: Marcus Ruddick

||

Answers:

so my code was actually ok, what was happening was a race condition where python was sending requests to connect before the database was fully initialized and accepting connections.

Answered By: Marcus Ruddick