在 psycopg2 中没有 postgres 数据库的所有权限

Do not have all privileges of a postgres database in psycopg2

我正在尝试编写一个 python 函数来将一些数据添加到数据库中。

from psycopg2 import connect

username = "username"
pswd = "1234"
db_name = "project_db"
con = connect(user = username,
                      password = pswd,
                      host = "127.0.0.1",
                      port = "5432",
                      database = db_name )  
cursor = con.cursor()
query = "INSERT INTO test_(name) "\
        "VALUES ('my_name')"

cursor.execute(query)

结果是:psycopg2.errors.InsufficientPrivilege: permission denied for table test_ 。 我尝试使用此命令向我的用户授予所有权限:

GRANT ALL PRIVILEGES ON DATABASE project_db to username;

然后重启数据库,结果还是一样

授予对数据库的权限既不会授予对数据库架构的权限,也不会授予对架构中对象的权限。

您需要使用特定的 GRANT 语句授予对架构和架构对象的权限。

Privileges section in PG docs