Calling the psql command without selecting any database
Try setting the database: psql -d postgres
:
echo `ALTER DATABASE x RENAME to y` | psql -d postgres
"template1" or "postgres" should be available.
See https://stackoverflow.com/questions/4483139/php-how-do-i-connect-to-postgresql-without-specifying-database-name