Forums

Can't connect remotly to Postgresql database

I've tried several times to connect to my DB via pgAdmin, via python but still have an error: "Could not connect to server: Operation timed out Is the server running on host "Referor-906.postgres.pythonanywhere-services.com" (10.0.0.70) and accepting TCP/IP connections on port 10906?"

My python query is con = psycopg2.connect(user='super', password='password', database='test', host='Referor-906.postgres.pythonanywhere-services.com', port='10906')

I have a paid account. What do I need to do?

You'll need to use an SSH tunnel to connect to your Postgres instance from outside PythonAnywhere -- the Postgres DB isn't directly accessible from the wider Internet for security reasons.

This pgAdmin help page explains how to set up an SSH tunnel (about halfway down the page) -- the "tunnel host" is ssh.pythonanywhere.com, the SSH host username is Referor (note that it's case-sensitive) and the SSH password is the one that you use to log in to the PythonAnywhere website (not your Postgres server password, which goes into the normal password field).

Thanks for your previous post. I've some progress. Now I have a new error :( 'OperationalError: FATAL: password authentication failed for user "referor"'

import psycopg2 import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0 sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder( ('ssh.pythonanywhere.com'), ssh_username='Referor', ssh_password='password', remote_bind_address=('**.postgres.pythonanywhere-services.com', 10906) ) as tunnel: params = { "dbname": 'test',
"user": 'referor', "password": 'password', "host": 'localhost', } connection = psycopg2.connect(
params) cursor = connection.cursor() cursor.execute("SELECT * from distributors") rows = cursor.fetchall() cursor.close() connection.close()

I already created user "referor" in my DB(I checked password twice) I've already tried to create new users and connect with super or postgres login.

Are you sure you're using the correctly cased username and password for each part? Make sure that you're using your PythonAnywhere username and password for the ssh tunnel and your postgres username and password for the database connection.

I've found a solution. I add "port": tunnel.local_bind_port to my "params"