Forums

remote connect to postgresql database

I understand that by default the listen address is localhost, however, var/lib/~/postgresql.conf can be configured to allow connection to the database using TCP/IP. The .conf file can't be accessed on pythonanywhere. Is there a workaround for remote pgdb access? Just curious, I have no dire need for this functionality.

That might be something we could support in future... The workaround would be to use SSH tunnelling, there's some info here: https://www.pythonanywhere.com/wiki/SSHTunnelling

It's probably worth mentioning that if you're trying to remote connect to a DB so that you can run analysis on your data and you're going to do that analysis in IPython, you can simply launch an IPython Notebook from your home directory on pythonanywhere. e.g.

import pandas as pd
import sqlalchemy

SQLALCHEMY_DATABASE_URI = "postgresql://super:yyy@xxx-66.postgres.pythonanywhere-services.com:10066/seekwell"
BASE_PATH = '/home/xxx/seekwell/'

db = sqlalchemy.create_engine(SQLALCHEMY_DATABASE_URI)

df = pd.read_sql(sql = 'SELECT * FROM events', con = db.engine)
do some stuff with your data ...

good suggestion!

Is it possible to connect to postgresql at pythonanywhere with Navicat? I don't have any success trying...

what's the steps you have taken?

eg: did you setup ssh tunnelling?

Data that I used: Connection Name: Anything Host Name/IP Address: the one from database page in my dashboard Port: from database page as well Initial Database: postgres User Name: super Password: the one I set up with postgres database for user 'super' SSH settings: Checkbox: checked hostname: ssh.pythonanywhere.com Port: 22 User: arnas158 Password : password that I use to login to this page.

Did I missed something here?

Is there an error that you get when you are doing that?

not anymore, just started working -_-, Still thanks!

Great! Do you know if you changed anything? I'm thinking maybe it was because you just upgraded and it took some time for the settings to apply.

Probably that's the case, didn't change anything.

connecting to postgres db using ssh tunnel and psycopg2:

import psycopg2
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('ssh.pythonanywhere.com'),
    ssh_username='pa_username',
    ssh_password='pa_password',
    remote_bind_address=('***.postgres.pythonanywhere-services.com', 10123)
) as tunnel:

    params = {
        "dbname": 'db_name',
        "user": 'db_username',
        "password": 'db_password',
        "host": 'localhost',
        "port": tunnel.local_bind_port,
    }

    connection = psycopg2.connect(**params)
    cursor = connection.cursor()

    cursor.close()
    connection.close()

Thanks!