Rails: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations

前端 未结 6 659
慢半拍i
慢半拍i 2021-02-07 01:48

I\'m trying to create the database in Rails. In Postgres I see the development and test database, however, I\'m getting a permissions error. I\'ve tried to follow this link, did

6条回答
  •  野性不改
    2021-02-07 02:17

    I had this issue when working on a Rails 6 application with PostgreSQL.

    The first check is to ensure that you've granted all privileges for a database to the particular user that you want using:

    GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myusername;
    

    But in my own case, the cause of the issue was that I created a Database and then granted all privileges to a particular user. After some time, I granted another user the privileges using GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myusername;

    So the second user even though I granted all privileges to it, didn't have permissions to perform actions on the database tables.

    Here's how I fixed it:

    Log into the PostgreSQL console where the database is stored:

    sudo -u postgres psql
    

    List all databases in that PostgreSQL database server:

    \l
    

    OR

    \list
    

    Connect to the database that you want to fix it's permissions:

    \c database_name
    

    OR

    \connect database name
    

    List all tables in the current database using your search_path:

    \dt
    

    OR

    List all tables in the current database regardless of your search_path:

    \dt *.
    

    You will notice that the tables still reference the initial user or role as the owner.

    Now you will have to modify the tables to reference the new user or role as the owner.

    You can modify each table individually using this:

    ALTER TABLE table_name OWNER TO new_owner;
    

    This does not require specifing the old_owner. It is essential when the user is postgres (the default database user) and you want to modify the owner to a new user.

    OR modify all the tables simultaneously using this:

    REASSIGN OWNED BY old_owner TO new_owner;
    

    This requires specifing the old_owner. It is essential when you have already modified the user from postgres (the default database user) to another user and you want to modify the owner to a new user.

    Note: Ensure that you are connected to the database that you want to modify privileges/permissions for, else you might run into errors.

    That's all.

    I hope this helps

提交回复
热议问题