8

I would like to su to the postgres user on Mac, so that I can run postgres.

What am I doing wrong here?

delirium:~ anna$ sudo su postgres
delirium:~ anna$

Nothing happens. If I try to run postgres, I then get something like this:

delirium:~ anna$ sudo pg_ctl -D /usr/local/var/postgres -l      
/usr/local/var/postgres/server.log startpg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

If I try this in order to su, I also get an error:

delirium:~ anna$ sudo su - postgres
su: no directory
delirium:~ anna$

What can I do instead? I'm fairly sure that the postgres user exists.

2 Answers 2

22

That looks consistent with the postgres user not having a login shell. Try sudo -u postgres bash

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

Comments

2

PostgreSQL is integrated in Server.app available through the App Store in Mountain Lion.

You would have this file: /Library/Server/PostgreSQL/Config/org.postgresql.postgres.plist

You can sudo to run shell commands as the owner (note the underscore in the username):

sudo -u _postgres createdb the_database -E UTF8

Or connect to the database with all grants:

psql -h localhost -U _postgres postgres

5 Comments

What is the difference between these two commands? Does any have benefit over other?
The sudo command allows user _postgres more than just connecting to an existing databse, for instance to create a new database.
With help of psql you also will be connected to database as root and will be able to create databases, will not?
psql is a command that connects to the database, within that scope you can do certain things to manage the database. If you switch to the postgres user, you can run other commands as well under that login account, like make directories, edit files, etc.
So if I plan to run only SQL it will be enough to psql -U postgres -c "..." instead of sudo -u postgres psql -c "...". Is this true?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.