Forums

Postgres Connection dropped.

HI

My application has been running pretty well using a postgres/flask combo on a simple localhost connection.

Recently however the connection seems to be failing, and not sure why as nothing has changed that would have caused this issue,

The error logs suggest that the instance of postgres is down:

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

However my ssh tunnel on my local machine is connecting fine and i've tried restarting postgres from the console but hit supposed permissions error.

I've seen some people connecting through their external hostnames, however I can't see why connecting through the localhost in this case would just stop working?

Thanks!

I'm a bit confused -- on PythonAnywhere, Postgres connections need to go to a different hostname (something.postgres.pythonanywhere-services.com), and connections via localhost just won't connect to anything.

Yeah. I got it sorted. Though seems weird how it always ran on the localhost till this point.

Like you said it doesn't make sense.

Cheers though!

It is weird, yes! Well, I suppose the most important thing is that it's working now...

I can query in the Postgres console, but in flask getting: Is the server running on host "localhost" (127.0.0.1) and accepting connections on port 5432? ... File "/usr/local/lib/python3.9/site-packages/psycopg2/init.py", line 127, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

My connection string is: 'postgresql://super:secret@magula10-2617.postgres.pythonanywhere-services.com:12617/postgres' (I tried it without the port)

Michael.sweeney303@gmail.com gratias

How did you set up the db connection in your web app?

Hello Please i am facing this issue when connecting postgres outside pythonanywhere

INFO:paramiko.transport:Authentication (password) successful! DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes DEBUG:paramiko.transport:Received global request "hostkeys-00@openssh.com" DEBUG:paramiko.transport:Rejecting "hostkeys-00@openssh.com" global request from server. 2023-10-26 13:32:26,456| ERROR | Secsh channel 0 open FAILED: open failed: Administratively prohibited ERROR:paramiko.transport:Secsh channel 0 open FAILED: open failed: Administratively prohibited 2023-10-26 13:32:26,456| ERROR | Could not establish connection from local ('127.0.0.1', 5500) to remote ('mydb.postgres.pythonanywhere-services.com', 13428) side of the tunnel: open new channel ssh error: ChannelException(1, 'Administratively prohibited') ERROR:sshtunnel.SSHTunnelForwarder:Could not establish connection from local ('127.0.0.1', 5500) to remote ('mydb.postgres.pythonanywhere-services.com', 13428) side of the tunnel: open new channel ssh error: ChannelException(1, 'Administratively prohibited') DEBUG:paramiko.transport:EOF in transport thread Traceback (most recent call last): File "\vue\db.py", line 22, in <module> connection = psycopg2.connect( ^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\psycopg2__init__.py", line 122, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ psycopg2.OperationalError: connection to server at "127.0.0.1", port 5500 failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

That looks like something on your machine, like an anti-virus or firewall is preventing the SSH tunnel from connecting.