N
Neon12mo ago
wise-white

Connection refused using pythonanywhere

Hello, I'm trying to host my python Flask application using pythonanywhere, but I'm running into an issue using Neon postgresql database. For some reason all works good while running the application on localhost, but once I try to start the app on pythonanywhere and try to read or create an entry from the database I get this error with Connection refused and don't really know why?
6 Replies
wise-white
wise-whiteOP12mo ago
error log
2024-11-25 23:48:09,289: Exception on /explore [GET]
psycopg2.OperationalError: connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (3.126.212.11), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (52.57.171.9), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (3.124.121.135), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a20:baca:a828:c69b:8952), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a0a:2022:d2c:7ebc:587b), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a1f:8076:d70b:eb80:8057), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
**NO MATCH**
2024-11-25 23:48:09,289: Exception on /explore [GET]
psycopg2.OperationalError: connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (3.126.212.11), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (52.57.171.9), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (3.124.121.135), port 5432 failed: Connection refused
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a20:baca:a828:c69b:8952), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a0a:2022:d2c:7ebc:587b), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
connection to server at "ep-ancient-cake-a2au5gcb.eu-central-1.aws.neon.tech" (2a05:d014:aa3:7a1f:8076:d70b:eb80:8057), port 5432 failed: Network is unreachable
#011Is the server running on that host and accepting TCP/IP connections?
**NO MATCH**
Shortened as I can't post the full error message Code
DATABASE_URL = os.environ.get('DATABASE_URL')
def get_db():
conn = psycopg2.connect(DATABASE_URL, sslmode='require', cursor_factory=DictCursor)
return conn

@app.route('/explore')
def explore():
with get_db() as conn:
with conn.cursor() as cur:
cur.execute("""
SELECT e.id, e.title, e.custom_url, u.username
FROM entries e
JOIN users u ON e.user_id = u.id
WHERE e.public = TRUE
ORDER BY e.id DESC
""")
public_entries = cur.fetchall()
return render_template('explore.html', public_entries=public_entries)
DATABASE_URL = os.environ.get('DATABASE_URL')
def get_db():
conn = psycopg2.connect(DATABASE_URL, sslmode='require', cursor_factory=DictCursor)
return conn

@app.route('/explore')
def explore():
with get_db() as conn:
with conn.cursor() as cur:
cur.execute("""
SELECT e.id, e.title, e.custom_url, u.username
FROM entries e
JOIN users u ON e.user_id = u.id
WHERE e.public = TRUE
ORDER BY e.id DESC
""")
public_entries = cur.fetchall()
return render_template('explore.html', public_entries=public_entries)
rival-black
rival-black12mo ago
After a quick search, I came across this page
If you want to use a Postgres server run by some other service Just upgrade to any paid PythonAnywhere plan -- the cheapest one will work just fine. You don't need to check the "Postgres" option -- that is for creating a PythonAnywhere-hosted Postgres server, which of course you don't need. Paid PythonAnywhere accounts have unrestricted Internet access so once the upgrade has gone through, you will be able to connect to your remote Postgres instance.
Are you connecting from a paid account? https://help.pythonanywhere.com/pages/Postgres/
PythonAnywhere help
Can I use Postgres on PythonAnywhere?
Yes! But you need a paying account. If you want to use a Postgres server run by us: If you have a free account, go to the Account page, and click on one of the buttons for upgrading to a paid acc
wise-white
wise-whiteOP12mo ago
Ah I see, I though that only applies only to their version of hosted Postgres...
yappiest-sapphire
yappiest-sapphire11mo ago
I have a similar issue and a paid PythonAnywhere (PA) account (not including their PostgreSQL option). The error message I get boils down to this:
SSL error: tlsv1 alert access denied
SSL error: tlsv1 alert access denied
, even when
sslmode=required
sslmode=required
. My working hypothesis is that the Postgresql library available on PA is too outdated to support the Neon services' security requirements. According to
pg_config --version
pg_config --version
, the version available is 12.11 ... which is beyond EOL. BTW: This is how my connection string is generated:
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{Config['DATABASE']['DB_USER']}:{Config['DATABASE']['DB_PASSWORD']}@{Config['DATABASE']['DB_HOST']}/{Config['DATABASE']['DB_NAME']}?sslmode=require&options=endpoint%3D{Config['DATABASE']['ENDPOINT_ID']}"
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{Config['DATABASE']['DB_USER']}:{Config['DATABASE']['DB_PASSWORD']}@{Config['DATABASE']['DB_HOST']}/{Config['DATABASE']['DB_NAME']}?sslmode=require&options=endpoint%3D{Config['DATABASE']['ENDPOINT_ID']}"
Note the endpoint-option, which is described here: https://neon.tech/docs/connect/connection-errors#the-endpoint-id-is-not-specified
Neon
Connection errors - Neon Docs
This topic describes how to resolve connection errors you may encounter when using Neon. The errors covered include The endpoint ID is not specified Password authentication failed for user Couldn't co...
rival-black
rival-black11mo ago
Hmm, I'd get in touch with the PythonAnywhere team
yappiest-sapphire
yappiest-sapphire11mo ago
Yeah, I emailed them. Here is an excerpt of my question: «(…) The server I am connecting to is running version 16, but from what I can find, PythonAnywhere is running version 12.11 - which is EOL according to postgresql.org and as far as I can tell does not support the security requirements for connecting to the server I am using. When will you upgrade and to what version?» And their response: «Hi there, I'm afraid it's not available yet. I can't say exactly when it will be. All the best, [REDACTED]»

Did you find this page helpful?