[FIXED] Flask-SQLAlchemy Postgres Error – could not connect to server: Connection refused (0x0000274D/10061)

Issue

This is my current init file, I already have the database model created on my models.py, but I don’t think I have any issues with that. I just can’t create my database table whenever I "db.create_all()" … It gives me the error, I posted below.

  • I have psycopg2 installed.
  • I tried adding "localhost:5432" on my database_uri link, but that doesn’t work either.
  • I went into my postgres config file and changed "listen_addresses" from "*" to "::1, 127.0.0.1"
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SECRET_KEY'] = 'mysecretkey'

# DATABASE CONFIGS ---------------------------------------------------------------------------
db = SQLAlchemy(app) 

ENV = 'dev'

if ENV == 'dev':
    app.debug = True
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:123456@localhost/flaskqna'
else:
    app.debug = False
    app.config['SQLALCHEMY_DATABASE_URI'] = ''

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

# DATABASE CONFIGS ---------------------------------------------------------------------------

from flaskqna import routes

THE ERROR

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?

Solution

I was stuck on this exact same problem, what worked for me was to explicitly use port 5433.

postgresql://postgres:123456@localhost:5433/flaskqna

I realized this after opening SQL Shell (psql) and entering 5432 for the first prompt and then the name of my database for the second prompt. In your case it would be flaskqna. The port 5433 appeared there.

It looks like you have reverted back to sqlite, but give it a shot

Answered By – Bill

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published