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

Question:

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?
Asked By: Ovicron

||

Answers:

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
'postgresql://postgres:123456@localhost/flaskqna'

replace localhost with your machine IP address from Wi-Fi setting (Network setting)

it will fix this error.

Answered By: Abdullah

I’m grateful for Abdullah’s answer, which led me to my solution.

When troubleshooting postgresql connection problems, the message is important:

If you get "connection refused," it means either that your server’s not running/you’re trying it at the wrong port OR your listen_addresses setting in postgresgl.conf doesn’t include the address from which you’re connecting.

If your postgresql server is listening on the address/net from which you’re trying to connect, but the access/user/database you’re requesting isn’t permitted by your pg_hba.conf, then you’ll get a different error, citing a lack in pg_hba.conf.

I configured my basement postgresql server in order to serve python apps running on my upstairs computer, so it was only listening on its LAN address. Then I found a service I wanted to install on the same linux box, and was baffled when it couldn’t connect locally. Trying the advice of replacing localhost with the LAN IP changed the message (almost always a good sign, right?) and got me to the solution.

Hoping my adventure helps someone else.

Answered By: David Peel

Start the server

pg_ctl -D "C:Program FilesPostgreSQL14data" start

Then run it again

Answered By: Ayomide Adedeji