1
votes

Just started using postgreSQL today alongside Python. I am trying to execute a query using the 'psycopg2' library but I am getting the following error: 'ProgrammingError: relation "users" does not exist'

In my pSQL cmd line I have can see my 'users' table all lowercase no capitalisation going on so not sure what's going on.. I will paste my query below. Thank you in advance for any help guys!

conn = psycopg2.connect(host="127.0.0.1", database="test", user="postgres", password="password")
    cur = conn.cursor()
    cur.execute('SELECT * FROM users WHERE username = %s AND password = %s', (username, password))

Note When I run \dt I get the following

         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | users | table | postgres
1
Do you have users table on database 'test'? - Vsevolod
In addition to @Vsevolod's comment - make sure that this table is in the public schema, or else you have to include the schema name as well. - Samuil Petrov
Hey guys yes the users table is present in the database and yes the table is in the public schema - Jackthomson
please run psql -h 127.0.0.1 test -U postgres -c "SELECT * FROM users limit 1" - Vao Tsun
Are you sure that you connect to same database? - Bartek Jablonski

1 Answers

-1
votes

This happened to me at work once, and I was informed that you probably should try the following:

python manage.py makemigrations, followed by python manage.py migrate. In my case there were no migrations to make but it has fixed my "relation does not exist" issue a few times!