Switch to postgres user on Mac
Asked Answered
C

2

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.

Charlatanry answered 25/5, 2012 at 11:6 Comment(0)
P
22

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

Pang answered 25/5, 2012 at 11:14 Comment(0)
L
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
Loritalorn answered 17/5, 2013 at 22:37 Comment(5)
What is the difference between these two commands? Does any have benefit over other?Exclave
The sudo command allows user _postgres more than just connecting to an existing databse, for instance to create a new database.Loritalorn
With help of psql you also will be connected to database as root and will be able to create databases, will not?Exclave
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.Loritalorn
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?Exclave

© 2022 - 2024 — McMap. All rights reserved.