psql: FATAL: database "<user>" does not exist

Ryan Rich picture Ryan Rich · Jul 13, 2013 · Viewed 376.8k times · Source

I'm using the PostgreSql app for mac (http://postgresapp.com/). I've used it in the past on other machines but it's giving me some trouble when installing on my macbook. I've installed the application and I ran:

psql -h localhost

It returns:

psql: FATAL:  database "<user>" does not exist

It seems I can't even run the console to create the database that it's attempting to find. The same thing happens when I just run:

psql 

or if I launch psql from the application drop down menu:

Machine stats:

  • OSX 10.8.4

  • psql (PostgreSQL) 9.2.4

Any help is appreciated.

I've also attempted to install PostgreSql via homebrew and I'm getting the same issue. I've also read the applications documentation page that states:

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. The default user is $USER, with no password.

So it would seem the application is not creating $USER however I've installed->uninstalled-reinstalled several times now so it must be something with my machine.

I found the answer but I'm not sure exactly how it works as the user who answered on this thread -> Getting Postgresql Running In Mac: Database "postgres" does not exist didn't follow up. I used the following command to get psql to open:

psql -d template1

I'll leave this one unanswered until someone can provide an explanation for why this works.

Answer

Kirk Roybal picture Kirk Roybal · Jul 30, 2013

It appears that your package manager failed to create the database named $user for you. The reason that

psql -d template1

works for you is that template1 is a database created by postgres itself, and is present on all installations. You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:

createdb

and then see if you can log in again with

psql -h localhost

This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.