Postgres user does not exist? - macos

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.

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.

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

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]]

I get exactly the same errors as kryshah with su - postgres and sudo -u postgres psql.
DanielM's answer gives also errors.
Outputs when wrong settings
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.
To get right settings and outputs
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.

the discussion and answer here was massively helpful to me:
psql: FATAL: database "<user>" does not exist

You can try this to create a new role, you can name :
sudo adduser <new_user_role>

The solution is simple:
log in as root
and after:
su - postgres
psql

Related

PSQL always return role doesn't exist, how fix?

When i use psql -U *anyusername* postgres it returns error:
role "username" does not exist.
But when I login as superuser and create user by:
create user *username* with password 'somepassword';
it always returns: CREATE ROLE.
Maybe I missed something miss, or is it a problem with my OS?
Info about environment:
Windows 10
psql version (9.5.18)
UPD:
results of \l and \du commands.

Role "postgres" doesn't exist on Windows 7

When running the following command after pg_ctl initdb and pg_ctl start commands:
C:\Program Files\PostgreSQL\11\bin>psql.exe -U postgres -h localhost
I am getting the following error:
role "postgres" doesn't exist
All similar questions are about Linux or Mac and they don't help me much, so I don't know how to fix it.
Since you didn't specify a username with -U when you ran initdb, the bootstrap superuser has the name of the operating system user with which you ran initdb.
So you can simply connect with
psql
If you would like to change the name of the user to postgres, that is simple:
-- get the name of the current user in a variable
SELECT current_user \gset
CREATE ROLE dummy SUPERUSER;
-- connect as user "dummy"
\c - dummy
-- rename the bootstrap user to "postgres"
ALTER ROLE :"current_user" RENAME TO postgres;
-- remove "dummy"
\c - postgres
DROP ROLE dummy;
-- disconnect
\q

Access Denied for User ODBC # localhost

i have installed Mysql on Windows7, i used mysql with mysql command line client & i also use Mysqldump in windows cmd, & it was working without any problem. But today, i tried to export database using mysqldump with this command in cmd
mysqldump –u root -p mypassword db_name > f:\mydb.sql
i tried many other commands and i always see error
Access Denied for User 'ODBC'#'localhost' (using password: yes) when trying to connect
as you can see, in mysqldump command i am using root as user then why i get user ODBC error ? one more thing, using mysql command line client i am still using mysql normally without any problem using root as user. i also tried to login in cmd with this command
mysql –u root -p mypassword
but still same error. and my password is 100% correct. kindly tell me how to solve this problem. Thanks
Have you tried storing pass in the cfg file? http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
try from command line:
mysql -u root -p
and then, when you are being asked for password, just type it.
Try the same with mysqldump command without typing your password after -p.

cannot connect to template1: FATAL: role "_postgres" does not exist

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 try to follow the OSM tutorial on psql and PostGis. But that seems rather hard... I am stuck here.
They suggest to create the user like:
sudo -u postgres createuser christoph
That did not work for me. I created the user like this:
$ sudo -u _postgres createuser -U postgres christoph
That worked without any problems. Then I tried this:
$ sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
> cannot connect to template1: FATAL: role "_postgres" does not exist
How can _postgres not exist if I just created a role with it?!
I also do not understand why
$ createdb mydb
> -bash: createdb: command not found
does not work although
I added the path like:
$ export PSQL_DIR=/usr/local/pgsql/bin/
$ export PATH="$PSQL_DIR:$PATH"
before.
Executing the command like:
$ /usr/local/pgsql/bin/createdb mydb
> createdb: cannot create database: ERROR: permission denied to create database
I am looged in with the user who installed psql...
Is that connected?!
It is very easy to confuse system users (stored in /etc/passwd) with Postgres roles (stored inside the database cluster).
In your first command, you mention both types of user:
sudo -u _postgres means "use the system user _postgres to execute the following command"
the -U postgres in the command means "connect to the Postgres cluster with the role postgres"
However, in the next command:
sudo -u _postgres createdb --encoding=UTF8 --owner=christoph gis
You specify the same sudo -u _postgres, but are missing the -U argument to the actual Postgres command (you say you want christoph to own the new DB, but you don't say "I am christoph").
The Postgres command-line tools have a default behaviour of guessing that the Postgres role name is the same as the system user name. Since the system user is named _postgres (with leading underscore) and the Postgres role is named postgres (no leading underscore), this assumption fails, and you can't connect.

Cannot Run Postgres (psql) locally

Whenever I try to run psql locally i.e. $ psql -d template1 -U postgres I receive -bash: psql: command not found
I've installed the Postgres Mac app, but when I click open psql I get...
Last login: Tue Jan 6 16:30:17 on ttys001
admins-MacBook-Air:~ surajkapoor$ /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit;
psql: FATAL: database "surajkapoor" does not exist
logout
[Process completed]
This doesn't make sense to me. I assumed installing the mac app install postgres locally.
You need to add the directory where the psql file is to the PATH variable of the mac which would be
/Applications/Postgres.app/Contents/MacOS/bin/
Opening psql without parameters will attempt to connect to a database with the name of the current user, if you don't want to add the directory to the PATH variable you can just put the whole address on the console and it should work
/Applications/Postgres.app/Contents/MacOS/bin/psql -d template1 -U postgres

Resources