Pregunta

If I run the following:

psql -h localhost -U admin -c "CREATE DATABASE sales OWNER admin;"

It returns:

psql FATAL: database "admin" does not exist

I need to use password authentication and have setup .pgpass as follows:

localhost:*:*:admin:admin

Any ideas?

¿Fue útil?

Solución

By default, when you connect as a user, the database connected to is the DB of the same name as the user.

If you want to connect to a different DB, you must specify it in the psql command line. Since in this case you're trying to create the DB, you can't connect to it yet, you must connect to another DB (like template1 or postgres) that already exists.

E.g.

psql -h localhost -U admin template1 -c "CREATE DATABASE sales OWNER admin;"
                           ^^^^^^^^^
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top