Skip to content

Instantly share code, notes, and snippets.

@je2ryw
Forked from checco/postgres-owner-grants.sql
Created July 4, 2021 03:05
Show Gist options
  • Save je2ryw/1e2c4a0a4da1958103ed4f788e6a9691 to your computer and use it in GitHub Desktop.
Save je2ryw/1e2c4a0a4da1958103ed4f788e6a9691 to your computer and use it in GitHub Desktop.

Revisions

  1. Francesco Latini revised this gist Oct 21, 2019. No changes.
  2. Francesco Latini revised this gist Oct 2, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions postgres-owner-grants.sql
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ ALTER SCHEMA other_schema OWNER TO user;
    -- --------------+--------+-------+----------------
    -- other_schema | table1 | table | other_user
    -- other_schema | table2 | table | user
    -- other_schema | table3 | table | other_user
    -- other_schema | table3 | table | other_user1

    --
    -- Change table owner
    @@ -61,11 +61,13 @@ ALTER TABLE other_schema.table1 OWNER TO user;
    -- --------------+--------+-------+----------------
    -- other_schema | table1 | table | user
    -- other_schema | table2 | table | user
    -- other_schema | table3 | table | other_user
    -- other_schema | table3 | table | other_user1

    --
    -- Change the ownership of database objects owned by a database role
    -- It will change the ownership only for the current database, not for all instance databases
    --

    REASSIGN OWNED BY other_user TO user;
    REASSIGN OWNED BY other_user TO user;
    -- or you can change more role at once
    REASSIGN OWNED BY other_user, other_user1 TO user;
  3. Francesco Latini created this gist Oct 2, 2019.
    71 changes: 71 additions & 0 deletions postgres-owner-grants.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    --
    -- Change database owner
    --

    ALTER DATABASE "db_name" OWNER TO user;

    --
    -- List schemas
    --

    -- db_name=> \dn
    -- List of schemas
    -- Name | Owner
    -- --------------------+-------------------------
    -- public | other_user
    -- other_schema | other_user

    --
    -- Change schema owner
    --

    ALTER SCHEMA other_schema OWNER TO user;

    --
    -- Check schema changes
    --

    -- db_name=> \dn
    -- List of schemas
    -- Name | Owner
    -- --------------------+-------------------------
    -- public | other_user
    -- other_schema | user

    --
    -- List schema tables
    --

    -- db_name=> \dt other_schema.*
    -- List of relations
    -- Schema | Name | Type | Owner
    -- --------------+--------+-------+----------------
    -- other_schema | table1 | table | other_user
    -- other_schema | table2 | table | user
    -- other_schema | table3 | table | other_user

    --
    -- Change table owner
    --

    ALTER TABLE other_schema.table1 OWNER TO user;


    --
    -- Check schema table changes
    --

    -- db_name=> \dt other_schema.*
    -- List of relations
    -- Schema | Name | Type | Owner
    -- --------------+--------+-------+----------------
    -- other_schema | table1 | table | user
    -- other_schema | table2 | table | user
    -- other_schema | table3 | table | other_user

    --
    -- Change the ownership of database objects owned by a database role
    -- It will change the ownership only for the current database, not for all instance databases
    --

    REASSIGN OWNED BY other_user TO user;