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.
This question is related to
macos
postgresql
psql -U postgres
Worked fine for me in case of db name: postgres & username: postgres. So you do not need to write sudo.
And in the case other db, you may try
psql -U yourdb postgres
As it is given in Postgres help:
psql [OPTION]... [DBNAME [USERNAME]]
The solution is simple:
log in as root
and after:
su - postgres
psql
I get exactly the same errors as kryshah with su - postgres
and sudo -u postgres psql
.
DanielM's answer gives also errors.
Answer however from przbabu's comment.
masi$ psql
psql: FATAL: database "masi" does not exist
masi$ psql -U postgres
psql: FATAL: role "postgres" does not exist
masi$ psql postgres
psql (9.4.1)
Type "help" for help.
I think the some part of this problem may be in owner settings in OSX
masi$ ls -al /Users/
total 0
drwxr-xr-x 7 root admin 238 Jul 3 09:50 .
drwxr-xr-x 37 root wheel 1326 Jul 2 19:02 ..
-rw-r--r-- 1 root wheel 0 Sep 10 2014 .localized
drwxrwxrwt 7 root wheel 238 Apr 9 19:49 Shared
drwxr-xr-x 2 root admin 68 Jul 3 09:50 postgres
drwxr-xr-x+ 71 masi staff 2414 Jul 3 09:50 masi
but doing sudo chown -R postgres:staff /Users/postgres
gives chown: invalid user: ‘postgres:staff’
.
In short, this is not the solution the problem. Use the tools provided by the postgres installation to create a user and database.
There are specific commands after postgres installation to add a new user to the database system. After initdb, run the following as described here
createuser --pwprompt postgres
createdb -Opostgres -Eutf8 masi_development
psql -U postgres -W masi_development
To avoid the password request all the time, you have three choices as described here.
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.
the discussion and answer here was massively helpful to me:
For me This was the solution on macOS ReInstall the psql
brew install postgres
Start PostgreSQL server
pg_ctl -D /usr/local/var/postgres start
Initialize DB
initdb /usr/local/var/postgres
If this command throws an error the rm the old database file and re-run the above command
rm -r /usr/local/var/postgres
Create a new database
createdb postgres_test
psql -W postegres_test
You will be logged into this db and can create a user in here to login
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.
Source: Stackoverflow.com