sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused

After starting up postgres on docker any attempt to connect to the database results in the following;

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

Background on this error at: http://sqlalche.me/e/e3q8

I have looked at similar issues and my ports are assigned as such;

ports:
 - 5432:5432

Details

  • Flask server using venv
  • Windows 10

Additional Details

Dockerfile (for postgres)

FROM postgres:9.6-alpine

COPY init-db /docker-entrypoint-initdb.d/

application file (FLASK_APP) THIS IS THE COMMAND THAT BREAKS IT

@application.cli.command(with_appcontext=True)
def create_db():
    db.engine.execute("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;")

    db.drop_all()
    db.create_all()

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f04ca2725c96 taxai_ml “gunicorn –worker-c…” 3 hours ago Up 3 hours 0.0.0.0:8900->5000/tcp taxai_ml_1
032fd31432af stripemock/stripe-mock “/stripe-mock -http-…” 3 hours ago Up 3 hours 0.0.0.0:12111-12112->12111-12112/tcp taxai_stripe_1
795b2ff4d7d5 redis:3.2.11-alpine “docker-entrypoint.s…” 3 hours ago Up 3 hours 0.0.0.0:6379->6379/tcp taxai_redis_1

Source: StackOverflow