Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active August 16, 2024 00:11
Show Gist options
  • Save drmalex07/e38b7a4e99e9feb5427ab21220aa1449 to your computer and use it in GitHub Desktop.
Save drmalex07/e38b7a4e99e9feb5427ab21220aa1449 to your computer and use it in GitHub Desktop.
Change database owner in Postgresql #postgresql #alter-database-set-owner

Say, that database "foo" is now owned by "user1"

Alter owner:

\c postgres
ALTER DATABASE "foo" OWNER TO "user2"

Reassign ownership (see also https://stackoverflow.com/a/13535184/1943126):

note: this will affect objects in current database ("foo") but also shared objects (databases,tablespaces)!!!

\c "foo"
REASSIGN OWNED BY "user1"  TO "user2" 

note: the following is probably not needed, covered by re-assignment of ownership

Revoke existing permissions from previous owner on all available schemas:

REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM "user1";
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA "bar" FROM "user1";
REVOKE USAGE ON SCHEMA "bar" FROM "user1";

Grant permissions on new owner:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "user2";
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA "bar" TO "user2";
GRANT USAGE ON SCHEMA "bar" TO "user2"

Check existing permissions on tables:

SELECT grantor, grantee, table_schema, table_name,privilege_type FROM information_schema.table_privileges
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment