Forums

Could not connect to server psycopg2

Hello,

I'm having troubles connecting to my PostgreSQL server.

This is the error that gets returned:

psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "xxxxxx" and accepting
        TCP/IP connections on port 5432?

Are you trying to connect to a Postgres database that is hosted somewhere else? Free accounts on PythonAnywhere have restricted Internet access; they can only make HTTP and HTTPS connections to external servers (and even then, they can only connect to sites on our whitelist). Postgres uses its own protocol, which is neither HTTP nor HTTPS.

In case I get a paid account is it possible to grant access to external Postgres database?

Our Postgres database is hosted on https://www.acugis.com/. Cheers

Paid accounts have unrestricted internet access. If your database provider is not blocking external access you could use it from paid account on PythonAnywhere.

Could you suggest a tier that would support Acugis (our host) and a very basic Python script? Thanks

what do you need to do with acugis on PythonAnywhere?

Any paid account.

I ran into the same issue. I have a paid account for custom domain. What should I do to fix it?

I see that we've been speaking about this over email, so we can continue the discussion there.

Same problem. I can (SQL) query my Postgres on python anywhere but flask not so good:

$ flask shell
>>> from hello import User
>>> u=User(username='Michael Grinberg')
>>> db.session.add(u)
>>> db.session.commit()

...

(psycopg2.OperationalError) could not connect to server: Connection refused
        Is the server running on host "magula10-2617.postgres.pythonanywhere-services.com" (10.0.0.35) 
and accepting
        TCP/IP connections on port 5432?
(Background on this error at: http://sqlalche.me/e/13/e3q8) (Background on this error at: http://sqlalc
he.me/e/13/7s2a)
>>>

added the port after the host and the insert went through:

'postgresql://super:secret@magula10-2617.postgres.pythonanywhere-services.com:12617/postgres'

...but my flask app not so good - error log:

2022-04-08 19:04:17,978: Error running WSGI application
2022-04-08 19:04:17,978: TypeError: 'module' object is not callable

This error doesn't look like being related to the db operations -- it looks like the WSGI file has some errors. See also: https://help.pythonanywhere.com/pages/DebuggingImportError/