password authentication failed for user "postgres" on mac - macos

I have problem creating new psql user because I cannot log in psql as "postgres", I have tried
1. sudo -u postgres psql
2. sudo -u postgres createuser img_site -P -s -e
and they are all ask for password of "postgres" which I don't know. I have tried to change unix password of user "postgres"(I know it's dangerous) and it still tells me: password authentication failed for user "postgres". I also have tried GUI pgAdmin but it's the same error.
I don't know if it's related: I have created a symbolic link
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
in order to get rid of error
createuser: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

Check pg_hba.conf. it should have a line like this at the top (before all other entries):
local all postgres peer
This allows local Unix-domain socket access by postgres db user to all databases with no password required.
Now clear and redefine the password for postgres system user (which is automatically created during PostgreSQL installation):
sudo passwd -d postrges
sudo su postgres -c passed
The special thing about this user account is that postgres server allows it to connect to the database, no questions asked.
Now, to define an explicit password for postgres db user using which you can login via other means than local Unix-domain socket connection, run:
su postgres -c psql template1
psql> ALTER USER postgres WITH PASSWORD '<password>';
You will be asked for the postgres system user account password before this command can be run. On successful completion, type \q to quit psql shell, and you are done with resetting the password for postgres db user.

sudo doesn't want the password of the account you're switching to, it wants the password of the account you're switching from. It also requires that you be an admin (or otherwise listead in /etc/sudoers). su, on the other hand, requires the password for the account you're switching to.

I was trying to setup postgres for Ruby on Rails and I was getting the the password authentication failed for user error. Check if the server is actually running:
pg_ctl -D /usr/local/var/postgres status
If you get
pg_ctl: no server running
Run
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Also you must include localhost in psql.
psql -U postgres -h localhost

This worked for me:
ALTER USER my_user WITH PASSWORD 'my_password';

I tried a lot of different tricks on my Macbook. And here is the one which helped me.On the screen, right click PostgreSQL14 for 'properties', then in 'connection' tab try to change port number from 5432 to 5433(or vise versa) -> save. And try to open again, for password use "postgres". Should work

Related

PostgreSQL keeps prompting for a password I never gave in PowerShell

I'm trying to set up postgresql to run locally on my windows machine. I've downloaded psql from postgresql.org and installed it. However, whenever I try to run the psql command from the command line it prompts me for a password that I never gave it for a user it seems to have automatically generated based on the user name of my computer.
In Powershell:
PS C:\Users\Theophilus> psql
Password:
psql: FATAL: password authentication failed for user "Theophilus"
Nothing I would use as a password works in this case. I've uninstalled and reinstalled postgres from my windows machine to see if maybe there was somewhere to input a password but I found nothing.
Additionally, running psql with the default username of 'postgres' results in the same problem.
PS C:\Users\Theophilus> psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
Hopefully someone can help me.
That's because you're not providing a username in the command so it's using your current username that you're logged in as which doesn't have permission. You should use the postgres account.
c:\path\to\psql.exe -U postgres
So in your case
PS C:\Users\Theophilus> psql -U postgres
https://wiki.postgresql.org/wiki/First_steps

Could not connect to database postgres: 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 installed the packages as admin on my computer. My username is christoph
The only way I can log in is via:
$ psql -U postgres
I want to create user called christoph, as my admin name on the computer.
I tried (from the terminal, without being "logged in into psql"):
$ sudo -u postgres createuser christoph
> sudo: unknown user: postgres
Then I read a little and tried (from the terminal, without being "logged in into psql"):
$ sudo -u _postgres createuser christoph
> Password: ****
> Could not connect to database postgres: FATAL: role "_postgres" does not exist
Why isn that working?
On recent version of OS X and with some installation methods the system user is created with a '_' prepended or appended to 'postgres', so the name of your system user is postgres_ or _postgres in your case, not 'postgres'. I don't use OS X, so I don't know what drives them to do this. Seems like they want to adhere to a naming schema for system accounts.
Not to be confused with the Postgres DB user (login role) of the name postgres. This mismatch causes all sorts of confusion. At least people become aware of the different meaning of some syntax elements ...
That's why you can log into Postgres via:
$ psql -U postgres
postgres is the DB role here, not the OS user.
But this won't work:
$ sudo -u postgres
Because there is no OS user of that name. Try instead:
$ sudo -u _postgres
But then peer authentication still won't work, because there is no DB user of the same name _postgres. Related answer:
Postgres user does not exist?
The authentication method activated by default in standard installations is peer authentication, where a system user on the local system has password-less access to a database role of the same name. That explains the last error message:
Could not connect to database postgres: FATAL: role "_postgres" does not exist
Your system tried to log into the DB with the same name as your current OS user using peer authentication, which fails due to the naming mismatch.
Your last command should work like this:
$ sudo -u _postgres createuser -U postgres christoph
The added -U postgres is an option to createuser specifying the DB role to log in with.
You still have to enter the password. I would consider using an entry in the a .pgpass file for password-less access, while the system user is different from the supposedly associated DB role.
Related:
Login Failed with Existing User on PostgreSQL
Run batch file with psql command without password

What is the default password for Postgres

I have just install Postgres 9.3 on Windows 7. The installation completed successfully. It has never asked me to provide the password for postgres user.
The service postgresql-x64-9.3 is up and running. However, I cannot connect: I do not not know the password. I've found the following answer, but it did not help:
similar question on Ubuntu
[LINUX]
might work for windows too
After installing postgres follow following steps in order to setup password for default system account of Linux execute following in terminal:
user:~$ sudo -i -u postgres
postgres#user:~$ psql
after executing above two commands you will get into postgres shell
Execute this query in postgres shell:
postgres=# ALTER USER postgres PASSWORD 'mynewpassword';
your new password is 'mynewpassword' without quotes and now you can connect with external GUI tools like DBeaver
WARNING: trust means exactly that. Anyone who can connect to the PostgreSQL server can control it. If you set trust mode that allows superusers like user postgres (or all users) to connect, they get total control of your PostgreSQL and can probably run shell commands too. You should usually only use it to change the password then restore the configuration back to the auth mode you were using before.
If you used an unattended installer script, the password will be in the script or associated config file.
Otherwise, treat it the same as if you lost/forgot the password rather than never knowing it:
Edit pg_hba.conf, setting the auth mode to trust instead of the default md5
In the Services control panel restart the PostgreSQL service
Connect with psql or PgAdmin or whatever
ALTER USER postgres PASSWORD 'mynewpassword';
Edit pg_hba.conf again and set the auth mode back to md5
Restart PostgreSQL again
pg_hba.conf is in your data directory. By default it'll be %PROGRAMFILES%\PostgreSQL\9.3\data.
To edit it you'll have to use the security tab to give yourself read/write permissions (via a UAC prompt). This might require you to set yourself as the owner of the file.
On unix systems it's more secure to prepend a
local all all peer
line to pg_hba.conf and then sudo -u postgres psql (assuming your PostgreSQL server runs as user postgres) to get an interactive psql session without using a password. That way you don't have to use trust.
On initialisation you can access the DB as:
Username: postgres
Password: postgres
By default user postgres does not have a password
Start psql and create a password:
sudo -u postgres psql
\password postgres - It will ask you enter a password for user postgres
Through trial and error I found that the password for Postgre SQL 10 for the username postgres is "admin". I kept typing in different password until I reached that password. I am using pgAdmin 4 to test out my SQL Statements, POSTGRE SQL 10 is the first server connection set up using localhost.
It seems there was no default password, but psql wouldn't accept a lack of a password (fe_sendauth: no password supplied). To get around this, I opened pgAdmin, then in the left sidebar:
Servers
Login/Group Roles
Right click postgres and click Properties,
Go to Definition tab
Set the password in the Password field
After saving, psql accepted that password. There may have been a switch I could have supplied to have it accept a lack of a password (--no-password?), but the user should probably have a password anyways, so this seemed reasonable.
go to control >> computer management >> Locaol users and group >> users >>
right click on openpgsvc >> set password.
after that now you can access with this password on openpgsvc
The simplest solution I've found is just to install PgAdmin and connect to the local server with the current Windows credentials (username + password). Then you can change the password to the postgres user.
step1: Go to control panel
Step2: Click on Administrative Tools
Step3: Click on Computer Management
Step4: There under "Local Users and Groups" Double click on user
Step:5: then right click on postgres and you can set password
refer this below image

Locked out of postgresql

createdb foo gives an invalid password for user (my username)
I can't login with
sudo psql
How do I reset my postgres user accounts?
Is it something I can do in the hba_conf file?
EDIT:
I was beginning a Postgres tutorial and wanted to have a fresh install. I ran
brew update
brew uninstall postgresql
brew install postgresql
pg_ctl -D some/path
createdb
This could be two things: you lost/misset the password, or Postgres might be configured in a way that prevents you from logging in.
Resetting the password:
Most of the time Postgres runs as user "postgres". Try su - postgres as root, and then run psql. If that doesn't work, you'll need to figure out what user postgres is running as, and su to that user. From there you can reset the password for the user. Also, make sure that your user is allowed to log in -- the default is to disable login for a new role .
Changing the configuration:
Find the pg_hba.conf file and edit it to permit password login. This file is usually loacted in /var/lib somewhere. On my Scientific Linux server its at: /var/lib/pgsql/9.2/data/pg_hba.conf, but on my Gentoo server its at /etc/postgresql-9.3/pg_hba.conf -- so locate pg_hba.conf might help you find it. This file is usually fairly well commented, and there is a manual page for it here: http://www.postgresql.org/docs/devel/static/auth-pg-hba-conf.html.

psql: FATAL: role "postgres" does not exist

I'm a postgres novice.
I installed the postgres.app for mac. I was playing around with the psql commands and I accidentally dropped the postgres database. I don't know what was in it.
I'm currently working on a tutorial: http://www.rosslaird.com/blog/building-a-project-with-mezzanine/
And I'm stuck at sudo -u postgres psql postgres
ERROR MESSAGE: psql: FATAL: role "postgres" does not exist
$ which psql
/Applications/Postgres.app/Contents/MacOS/bin/psql
This is what prints out of psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+------------+----------+---------+-------+---------------------------
user | user | UTF8 | en_US | en_US |
template0 | user | UTF8 | en_US | en_US | =c/user +
| | | | | user =CTc/user
template1 | user | UTF8 | en_US | en_US | =c/user +
| | | | | user =CTc/user
(3 rows)
So what are the steps I should take? Delete an everything related to psql and reinstall everything?
Thanks for the help guys!
NOTE: If you installed postgres using homebrew, see the comment from #user3402754 below.
Note that the error message does NOT talk about a missing database, it talks about a missing role. Later in the login process it might also stumble over the missing database.
But the first step is to check the missing role: What is the output within psql of the command \du ? On my Ubuntu system the relevant line looks like this:
List of roles
Role name | Attributes | Member of
-----------+-----------------------------------+-----------
postgres | Superuser, Create role, Create DB | {}
If there is not at least one role with superuser, then you have a problem :-)
If there is one, you can use that to login. And looking at the output of your \l command: The permissions for user on the template0 and template1 databases are the same as on my Ubuntu system for the superuser postgres. So I think your setup simple uses user as the superuser. So you could try this command to login:
sudo -u user psql user
If user is really the DB superuser you can create another DB superuser and a private, empty database for him:
CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
But since your postgres.app setup does not seem to do this, you also should not. Simple adapt the tutorial.
For MAC:
Install Homebrew
brew install postgres
initdb /usr/local/var/postgres
/usr/local/Cellar/postgresql/<version>/bin/createuser -s postgres or /usr/local/opt/postgres/bin/createuser -s postgres which will just use the latest version.
start postgres server manually: pg_ctl -D /usr/local/var/postgres start
To start server at startup
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Now, it is set up, login using psql -U postgres -h localhost or use PgAdmin for GUI.
By default user postgres will not have any login password.
Check this site for more articles like this: https://medium.com/#Nithanaroy/installing-postgres-on-mac-18f017c5d3f7
The key is "I installed the postgres.app for mac." This application sets up the local PostgreSQL installation with a database superuser whose role name is the same as your login (short) name.
When Postgres.app first starts up, it creates the $USER database,
which is the default database for psql when none is specified. The
default user is $USER, with no password.
Some scripts (e.g., a database backup created with pgdump on a Linux systsem) and tutorials will assume the superuser has the traditional role name of postgres.
You can make your local install look a bit more traditional and avoid these problems by doing a one time:
/Applications/Postgres.app/Contents/Versions/9.*/bin/createuser -s postgres
which will make those FATAL: role "postgres" does not exist go away.
createuser postgres --interactive
or make a superuser postgresl just with
createuser postgres -s
And if you are here in 2023 and wondering what works with the latest Postgres on the latest macOS (macOS Monterey )
follow this:
brew install postgresql
createuser -s postgres
brew services restart postgresql
This happens when you run initdb with a user whose ID is not postgres, without specifying the postgres username with --username=postgres or -U postgres.
The database cluster is then created with the system's user account that you used to run initdb, and it is given superuser permissions.
To fix it, simply create a new user named postgres with the option --superuser using the createuser utility that comes with Postgres. The utility can be found in the Postgres' bin directory. e.g.
createuser --superuser postgres
If you have a custom hostname or port then be sure to set the appropriate options.
Don't forget to delete the other user account that was created for you by initdb.
If you installed postgres from brew, run this in your terminal :
/usr/local/opt/postgres/bin/createuser -s postgres
First you need create a user:
sudo -u postgres createuser --superuser $USER
After you create a database:
sudo -u postgres createdb $USER
Change $USER to your system username.
You can see the the complete solution here.
I needed to unset $PGUSER:
$ unset PGUSER
$ createuser -s postgres
For me, this code worked:
/Applications/Postgres.app/Contents/Versions/9.4/bin/createuser -s postgres
it came from here:
http://talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4
If you installed postgres from Brew and are using an Apple Silicon (M1) mac, run this in your terminal:
/opt/homebrew/opt/postgresql/bin/createuser -s postgres
If you're using an Intel (x86) mac, run this in your terminal:
/usr/local/opt/postgres/bin/createuser -s postgres
Running this on the command line should fix it
/Applications/Postgres.app/Contents/Versions/9.4/bin/createdb <Mac OSX Username Here>
This article helped me to solve same issue psql: FATAL: role “postgres” does not exist.
I am using mac, so I entered this command in terminal:
createuser -s postgres
And it worked for me.
This worked for me
createuser -s postgres
note: I'm using mac catalina
If you're using docker, make sure you're NOT using POSTGRES_USER=something_else, as this variable is used by the standard image to know the name of the PostgreSQL admin user (default as postgres).
In my case, I was using this variable with the intent to set another user to my specific database, but it ended up of course changing the main PostgreSQL user.
We have a db named postgres after brew install postgresql and brew services start postgresql. So we can open psql like this by default.
psql postgres
And then we can add users with any name like this in that psql console.
CREATE USER postgres
And if we want a super user, then we can add SUPERUSER at the end.
For m1 chips, if you have not installed postgresql package by homebrew, install it in terminal with:
brew install postgre
then create a username manually by:
/opt/homebrew/bin/createuser -s <username>
your error is probably fixed; but if you occur the error
FATAL: database "databasename" does not exist
then you have to create your database manually by:
/opt/homebrew/bin/createdb -U <username> <databasename>
Dropping the postgres database doesn't really matter. This database is initially empty and its purpose is simply for the postgres user to have a kind of "home" to connect to, should it need one.
Still you may recreate it with the SQL command CREATE DATABASE postgres;
Note that the tutorial mentioned in the question is not written with postgres.app in mind.
Contrary to PostgreSQL for Unix in general, postgres.app tries to look like a normal application as opposed to a service that would be run by a dedicated postgres user having different privileges than your normal user. postgres.app is run and managed by your own account.
So instead of this command: sudo -u postgres psql -U postgres, it would be more in the spirit of postgres.app to just issue: psql, which automatically connects to a database matching your users's name, and with a db account of the same name that happens to be superuser, so it can do anything permissions-wise.
This is the only one that fixed it for me :
createuser -s -U $USER
For what it is worth, i have ubuntu and many packages installed and it went in conflict with it.
For me the right answer was:
sudo -i -u postgres-xc
psql
I've faced similar problem today, actually i was not sure what was the username. Here is the 2 thing, if you are under enterprise and don't have system admin access the postgres will create your enterprise username as the postgres admin username. If you install through Homebrew it will definitely happening. In that case simply run your psql service with brew and do an echo of the username
brew services start postgresql
then
echo $USER
You will see your username of the postgres user.
If you are experiencing this problem right after running a docker container try destroying the container and recreating it. That solved it for me:
docker-compose down
docker-compose up --force-recreate
This should recreate the db with postgresuser as default user
With a new mac (M1) and latest postgres (14.0) installed via homebrew, nothing helped me from this topic, but i just reinstalled postgres and it helped:
brew services stop postgresql
rm -rf /opt/homebrew/var/postgres/*
brew reinstall postgresql
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
brew services restart postgresql
So, it's a miracle or something like that...
Then just:
psql -d postgres
If you are a MAC (M1) user and installed the Postgres using HomeBrew then follow these steps:
Check your Postgres location using which psql
then run the command /opt/homebrew/bin/createuser -s postgres if the output for the first command is /opt/homebrew/bin/psql
The idea is to create a user named 'postgres' using the Postgres installation location. So you may need to change the command based on the location of your Postgres.
On Ubuntu system, I purged the PostgreSQL and re-installed it. All the databases are restored.
This solved the problem for me.
Advice - Take the backup of the databases to be on the safer side.
Context
I am adding an answer for a case I have not seen here, which is an edge case if you have multiple users on the same machine and the user who is trying to use postgres services is not the user who installed postgres on the machine.
What I have tried
Among other similar commands, for me all these commands failed:
createuser -s [your username]
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "[your username]" does not exist
createuser -s postgres
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "[your username]" does not exist
sudo -u postgres createuser --superuser [your username]
# sudo: unknown user: postgres
# sudo: error initializing audit plugin sudoers_audit
psql -U postgres
# psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist
Reason
The reason is because neither postgres role nor [your username] (aka whoami on your command line) are in postgres.
Solution
In such edge case I had to first login with the user who installed postgres:
sudo su - [username that installed postgres]
And then create a role for my new user:
createuser -s [your username]
On Mac, executing
createuser -s postgres
in a terminal worked.
I don't think that sudo is needed here because psql -l returns a list of databases. This tells me that initdb was run under the user's current user, not under the postgres user.
You can just:
psql
And continue the tutorial.
I would suggest A.H's general points of creating the postgres user and db because many applications may expect this to exist.
A brief explanation:
PostgreSQL will not run with administrative access to the operating system. Instead it runs with an ordinary user, and in order to support peer authentication (asking the OS who is trying to connect) it creates a user and db with the user that runs the initialization process. In this case it was your normal user.
I became stuck on this issue having executed brew services stop postgresql the day prior. The day following: brew services start postgresql would not work. This is because as is shown when you install using homebrew. postgresql uses a launchd ... which loads when your computer is powered on. resolution:brew services start postgresql Restart your computer.
The \du command return:
Role name = postgres#implicit_files
And that command postgres=# \password postgres return error:
ERROR: role "postgres" does not exist.
But that postgres=# \password postgres#implicit_files run fine.
Also after sudo -u postgres createuser -s postgres the first variant also work.

Resources