Search code examples
pythonpostgresqlpsycopg

Why is my created postgres user being denied access to a table?


I have created a user for my Postgres database but I cannot access the data with the user that I set up.

In a python shell I have run the following code:

>>> import psycopg2

>>> conn=psycopg2.connect(
database="db",
user="postgres",
host="/tmp/",
password="opensesame"
)

>>> cur = conn.cursor()

>>> state = 'Arkansas'

>>> cur.execute("SELECT city FROM table WHERE state = %s", (state,))

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    cur.execute("SELECT city FROM table WHERE state = %s", (state,))
ProgrammingError: permission denied for relation table

As the superuser, I have tried:

db=# ALTER USER postgres SET TO default

but it did not help

I need to get this user access to this table, any help would be appreciated. Thank you for viewing.


Solution

  • try this, log into postgres

    sudo -u postgres psql

    then enter \l it will list all the tables, you will see a list of your created databases,

    then

    GRANT ALL ON db TO postgres;

    if all goes well you should now do \l again and see that you are now privileged.