Calling the Psql Command Without Selecting Any Database

Calling the Psql Command Without Selecting Any Database

The syntax of the psql command line client is

psql [option...] [dbname [username]]

I am passing the command ALTER DATABASE x RENAME to y to this command:

echo `ALTER DATABASE x RENAME to y` | psql

Currently I am getting the error

psql: FATAL:  database "myuser" does not exist

It looks like the psql command tries to open the database with the same name as the current user name.

How can I start the psql command without selecting any database?

Edit:

A workaround is of course just to create an empty database for the user.

Using the database x as a parameter is not working, as this blocks the rename.

2 Answers

Try setting the database: psql -d postgres:

echo `ALTER DATABASE x RENAME to y` | psql -d postgres

"template1" or "postgres" should be available.

See

In Ubuntu:

sudo -u postgres psql
postgres=# ALTER DATABASE "old" RENAME TO "new";
ALTER DATABASE
postgres=# \q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.