How do I rename the default postgres superuser to "root"?

hobbes3 picture hobbes3 · Mar 7, 2012 · Viewed 17.8k times · Source

I currently log in to PostgreSQL using psql -U postgres. How do I rename postgres user to root?

If I am already logged in as postgres then trying ALTER USER postgres RENAME TO root will say ERROR: session user cannot be renamed.

Is it possible to rename the user without logging as postgres user? I don't think I have any other superuser since this is a fresh install of PostgreSQL.

By the way, I am running Gentoo on Amazon EC2.

Answer

Joachim Isaksson picture Joachim Isaksson · Mar 7, 2012

You should be able to just create a new postgres superuser called root by logging in as the postgres user and (at the shell) typing;

createuser --superuser root
psql> create database root owner root

After that, when logged in as root, you should be able to do what you want with the postgres user.