Reputation: 51
I am having trouble trying to make queries to a postgres db that is running in Docker.
As far as I can tell the database is connecting correctly using the psycopg2 library.
However when I execute a command and try to fetch the results, the results are empty. I can make the same query at the command line using psql and I get the expected results.
Can anyone help me figure out what I am doing wrong?
con = psycopg2.connect(
host= 'localhost',
port= '5432',
database= 'daystarr',
user= 'postgres',
password= 'admin',
)
print('Connected to Postgres Database')
cur = con.cursor()
cur.execute('SELECT * FROM tickets')
rows = cur.fetchall()
for r in rows:
print(r)
print(cur.fetchone())
cur.close()
con.close()
Here is the result when I run in command line:
cur.fetchall()
returns nothing
and cur.fetchone()
returns None
.
The other thing is that when I try to execute the command CREATE TABLE tickets (ticket_id INT PRIMARY KEY;)
I get a duplicate table error. So it must be connecting to the table. Thanks in advance!
Here is what it looks like when I just print(cur.fetchall())
:
Upvotes: 0
Views: 50
Reputation: 51
Okay, so this is a weird edge case but if you are looking at this in the future I have found what the problem was. I had a version of PostgreSQL installed and apparently listening on port 5432. My docker container was exposing this port also. So basically my app must have been taking priority on my local machine install. I uninstalled PostgreSQL from my local machine and it found the Docker container immediately. Thank you for anyone who helped.
Upvotes: 1
Reputation: 141
You are missing the ;
in your query, it should be
cur.execute("SELECT * FROM tickets;")
Upvotes: 0