326

How can I change ownership of a PostgreSQL database in phppgadmin?

3 Answers 3

555
ALTER DATABASE name OWNER TO new_owner;

See PostgreSQL manual's entry on this for more details.

Sign up to request clarification or add additional context in comments.

9 Comments

@mArtinko5MB: That's impossible, ALTER doesn't DROP a database.
@mArtinko5MB: Also impossible, ALTER TABLE doesn't DROP the table. Show us your SQL, something is badly broken in your statements.
Note, all tables and sequences inside the database will still be assigned to the original owner.
ERROR: must be member of role ... = DOES NOT WORK WITH RDS
I solved it, the issue was I needed to GRANT newrole TO ownerrole; as ownerrole had ADMIN option but not SET option, so couldn't set the newrole
|
76

Frank Heikens answer will only update database ownership. Often, you also want to update ownership of contained objects (including tables). Starting with Postgres 8.2, REASSIGN OWNED is available to simplify this task.

IMPORTANT EDIT!

Never use REASSIGN OWNED when the original role is postgres, this could damage your entire DB instance. The command will update all objects with a new owner, including system resources (postgres0, postgres1, etc.)


First, connect to admin database and update DB ownership:

psql
postgres=# REASSIGN OWNED BY old_name TO new_name;

This is a global equivalent of ALTER DATABASE command provided in Frank's answer, but instead of updating a particular DB, it change ownership of all DBs owned by 'old_name'.

The next step is to update tables ownership for each database:

psql old_name_db
old_name_db=# REASSIGN OWNED BY old_name TO new_name;

This must be performed on each DB owned by 'old_name'. The command will update ownership of all tables in the DB.

6 Comments

Nice! ...unless the owner is postgres, himself... Learned that the hard way.
The problem is that it does not change the owner of a single database, but it replaces the owner everywhere by the new one.
The above REASSIGN OWNED is going to change all the database (if there are multiple databases) in a same instance to the new role.
For an alternative to REASSIGN OWNED (typically because your owner is postgres), see the snippets in stackoverflow.com/a/2686185/1839209.
What about ALTER LARGE OBJECT large_object_oid OWNER TO new_owner ?
|
3

A slight change to answer proposed by Frank. While the query is correct, one may execute the query without making any changes to the ownership.

This happens when we login as the olduser and run psql in the terminal and do not specify the database.

Instead , after logging with older user.. type the dbname with psql

Terminal prompt will change. After running the command, you will see the output as "ALTER DATABASE". You can verify the ownership with '\list' command

$ psql <<dbname>>
dbname=# ALTER DATABASE name OWNER TO new_owner;

ALTER DATABASE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.