5

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?
4
  • 1
    Do you have a PostgreSQL database running somewhere? Commented Jul 23, 2020 at 0:14
  • Yes, I have pgAdmin4 installed and I created the database called "flaskqna" Commented Jul 23, 2020 at 0:16
  • 2
    Can you connect via psql? Commented Jul 23, 2020 at 1:45
  • No :( I gave up. I just switched back to sqlite instead of Postgres. Seems like my firewall or my PC has something wrong with network/ports. Commented Jul 23, 2020 at 16:20

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

Comments

1
'postgresql://postgres:123456@localhost/flaskqna'

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

it will fix this error.

Comments

0

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.

Comments

0

Start the server:

pg_ctl -D "C:\Program Files\PostgreSQL\14\data" start

Then run it again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.