4

Working on OS X 10.10, installed postgreSQL and PostGIS from here, psql vers 9.3.5. I am having a hard time getting postgreSQL running.

I installed the packages as admin on my computer. My username is christoph

The only way I can log in is via:

$ psql -U postgres

I want to create user called christoph, as my admin name on the computer. I tried (from the terminal, without being "logged in into psql"):

$ sudo -u postgres createuser christoph
> sudo: unknown user: postgres

Then I read a little and tried (from the terminal, without being "logged in into psql"):

$ sudo -u _postgres createuser christoph
> Password: ****
> Could not connect to database postgres: FATAL: role "_postgres" does not exist

Why isn that working?

1

1 Answer 1

9

On recent version of OS X and with some installation methods the system user is created with a '_' prepended or appended to 'postgres', so the name of your system user is postgres_ or _postgres in your case, not 'postgres'. I don't use OS X, so I don't know what drives them to do this. Seems like they want to adhere to a naming schema for system accounts. Not to be confused with the Postgres DB user (login role) of the name postgres. This mismatch causes all sorts of confusion. At least people become aware of the different meaning of some syntax elements ...

That's why you can log into Postgres via:

$ psql -U postgres

postgres is the DB role here, not the OS user.

But this won't work:

$ sudo -u postgres

Because there is no OS user of that name. Try instead:

$ sudo -u _postgres

But then peer authentication still won't work, because there is no DB user of the same name _postgres. Related answer:

The authentication method activated by default in standard installations is peer authentication, where a system user on the local system has password-less access to a database role of the same name. That explains the last error message:

Could not connect to database postgres: FATAL: role "_postgres" does not exist

Your system tried to log into the DB with the same name as your current OS user using peer authentication, which fails due to the naming mismatch.

Your last command should work like this:

$ sudo -u _postgres createuser -U postgres christoph

The added -U postgres is an option to createuser specifying the DB role to log in with.

You still have to enter the password. I would consider using an entry in the a .pgpass file for password-less access, while the system user is different from the supposedly associated DB role.

Related:

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your answer. I just realised that I have a horrible typo in there. I tried with _postgres which gave me the error message.
@Stoffer: My answer still applies. I was about to ask why you did not get another error message, which is clear now.
Erwin, I understand the problem and I dont. Sorry, I am very new databases and just got started. I underatand that there is a db user which would be postgresql and I understand that there is a system user _postgresql or postgresql_. I can log into the db with $ psql -U postgres because its the corresponding role to the db. Then it becomes tricky. I do not underatand the problem you describe under peer authentication...
Thanks! That worked without error. Could you explain why that worked? Do I have a user now for the db postgres or is it a user for which i can create databases now? Or, do you have a good, comprehensive document for someone who just started learning psql where all the difference are described?! I feel like lost in translation/lacking some crucial definitions of words... The documentation is not very helpful because it assumes already some knowledge.
@Stoffer: Peer authentication allows password-less access if the name of the OS user and the DB user match. Follow the links I provided for more explanation. DB roles are for whole DB clusters: Detailed explanation: stackoverflow.com/questions/24918367/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.