Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of connecting to postgresql? #2

Open
psychemedia opened this issue Jan 13, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@psychemedia
Copy link

commented Jan 13, 2015

I was wondering if it would be useful to have a simple notebook that shows how to connect in to the postgresql database, and perhaps also give an example of connecting to mongodb?

...which is to say: how do you connect to postgresql etc?

docker ps

calvingiles/data-science-environment:latest "/notebook.sh"  0.0.0.0:443->8888/tcp    calvingiledatscience_notebook_1      
postgres:latest  "/docker-entrypoint.    0.0.0.0:5433->5432/tcp   calvingiledatscience_devpostgres_1   

[UPDATED]
I found I could connect using:

import psycopg2
con = psycopg2.connect(host=IPADDRESS,port=5432,user='postgres',password='PGPass')

with IPADDRESS found by running fig run devpostgres env from the boot2docker commandline, though when I try using localhost I get an error?

@psychemedia

This comment has been minimized.

Copy link
Author

commented Jan 14, 2015

After a bit more digging, I tried setting the bindings as follows:

devpostgres:
    environment:
        - POSTGRES_PASSWORD
    image: postgres
    ports:
        - "0.0.0.0:5433:5432"
    volumes_from:
        - devpostgresdata

but this didn't help?

However, I found I could connect via the IP address of the docker host server:

In the notebook:

IPADDRESS=!netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'

import psycopg2
con = psycopg2.connect(host=IPADDRESS[0],port='5433',user='postgres', password="PGPass")

!pip3 install ipython-sql
%load_ext sql
postgrescon = 'postgresql://postgres:PGPass@'+IPADDRESS[0]+':5433'

#Then cell magic via:
%%sql $postgrescon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.