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!

Edit - I sorted it out. Solution near end of post. PEBCK.

Hi, I have been trying to connect to my postgresql database with ssh tunneling.

I connect - I think successfully with local command:

ssh -L 3333:nverse-825.postgres.pythonanywhere-services.com:3306 nverse@ssh.pythonanywhere.com

Which gives me:

<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess

And a request for a password. I get a terminal and a command prompt and a quick whoami removes any remaining existential difficulty. However whenever I try to connect I get a:

channel 3: open failed: connect failed: Connection refused

pop up in my brand new terminal. So something is happening.

Locally I get some kind of connection refused error depending on how I try to connect. I have tried a few ways.

Any help appreciated. Thanks.

Edit -- I tried with a different port number like this:

ssh -L 3333:nverse-825.postgres.pythonanywhere-services.com:5432 nverse@ssh.pythonanywhere.com

But still get the same error.

Edit again... Aaaaaand I try with the right port number... finally....

ssh -L 3333:nverse-825.postgres.pythonanywhere-services.com:10825 nverse@ssh.pythonanywhere.com

And it works. Sorry about that.... :)

Cool. Glad you got there.

Can I access my PA postgresql from my golang program on local linux? I am able to SSH: $ ssh -L 5432:magula10-1234.postgres.pythonanywhere-services.com:10123 magula10@ssh.pythonanywhere.com (prompts for pwd)

...but from a golang running on local linux?

Hi, I already responded to your email, but for reference we have a tutorial on how to achieve this using Python: https://help.pythonanywhere.com/pages/AccessingPostgresFromOutsidePythonAnywhere/