I have just installed Postgres and have been tinkering with it and various configurations for 1-2 hours.
I am stuck on being unable to change to the postgres-user
$ su - postgres
yields the following error: su: unknown login: postgres
$ sudo -u postgres psql
yields the following error: sudo: unknown user: postgres
These attempts are made as the normal user. Trying them as root has the exact same results. I have installed postgres via Homebrew on OS X, and I have read the instructions multple times.
initdb initializes the database cluster's default locale and character set encoding. The character set encoding, collation order ( LC_COLLATE ) and character set classes ( LC_CTYPE , e.g., upper, lower, digit) can be set separately for a database when it is created.
Connecting to a Database In order to connect to a database you need to know the name of your target database, the host name and port number of the server, and what user name you want to connect as. psql can be told about those parameters via command line options, namely -d , -h , -p , and -U respectively.
sudo -u postgres is running the rest of the command string as the UNIX user postgres. sudo psql -U postgres db -w is running the command as the UNIX user root and (presumeably) connecting to postgres as the user "postgres" Probably the . pgpass file doesn't exist for the unix user postgres.
Exiting psql Using a Meta-Command The meta-command for exiting psql is \q .
psql
: Logs me in with my default username
psql -U postgres
: Logs me in as the postgres user
Sudo doesn't seem to be required for me.
I use Postgres.app for my OS X postgres database. It removed the headache of making sure the installation was working and the database server was launched properly. Check it out here: http://postgresapp.com
Edit: Credit to @Erwin Brandstetter for correcting my use of the arguments.
By psql --help
, when you didn't set options for database name (without -d
option) it would be your username, if you didn't do -U
, the database username would be your username too, etc.
But by initdb
(to create the first database) command it doesn't have your username as any database name. It has a database named postgres
. The first database is always created by the initdb command when the data storage area is initialized. This database is called postgres.
So if you don't have another database named your username, you need to do psql -d postgres
for psql command to work. And it seems it gives -d
option by default, psql postgres
also works.
If you have created another database names the same to your username, (it should be done with createdb
) then you may command psql
only. And it seems the first database user name sets as your machine username by brew.
psql -d <first database name> -U <first database user name>
or,
psql -d postgres -U <your machine username>
psql -d postgres
would work by default.
OS X tends to prefix the system account names with "_"; you don't say what version of OS X you're using, but at least in 10.8 and 10.9 the _postgres user exists in a default install. Note that you won't be able to su
to this account (except as root), since it doesn't have a password. sudo -u _postgres
, on the other hand, should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With