Reputation: 3359
I'm having a little trouble. I've tried using psycopg2 and sqlalchemy to get into my local PostgreSQL database and, while it's not throwing an error, it won't make the connection. Instead it's just stuck executing the code. Here's where it's going wrong.
conn = psycopg2.connect(database='db', user="user", password="password", host="127.0.0.1", port="55212")
And likewise when using sqlalchemy here is where it's going wrong.
engine = create_engine('postgresql://pw:db@localhost:55212/DB')
Please help, I can't do anything until this is resolved!
Upvotes: 0
Views: 1423
Reputation: 118
Without seeing the logs, one can only guess.
But basically, when using sqlalchemy
your connection string that you pass to create_engine
should look like: "postgresql+psycopg2://user_name:password@host_address:port/schema"
.
If the data you passed is correct, make sure your DB server is open for connections.
If you are attempting to connect to a docker container from within a different docker container, make sure they share a network, and change host_address
from localhost to the name of the container.
The default port for PostgreSQL is 5432
so make sure what port to use before trying a different one.
After creating the Engine
, connect to the db with engine.connect()
, which returns a connection (preferably with a context manager).
You can then use that connection to execute
queries or pass it to pandas read_sql_query
method.
Upvotes: 1