Postgres won't let me log in - postgresql

I am trying to use postgres but when I type:
sudo -u postgres psql
I get prompted for a password, which I've tried 'postgres' and my root password. I get this error:
psql: FATAL: password authentication failed for user "postgres"
How can I reset postgres to not prompt for a password? I installed with homebrew, and I'd be just as happy uninstalling and reinstalling as changing the permissions.
I tried updating pg_hba.conf with this line to no success:
local all trust

The line in pg_hba.conf should be local all all trust.

Related

psql: FATAL: password authentication failed for user "postgres" postgresql version 10

I recently installed Postgre version 10 on a linux red hat. I'm trying to configure that psql will prompt a user for password when accessing the database. After changing everything to scram-sha-256. I'm getting this error when accessing psql
psql: FATAL: password authentication failed for user "postgres"
This is my pg_hba.conf:
Any idea how can I fix this? Thanks!
Postgres store password md5 format as default. If you want to change password encryption you have to follow bellow solution:
P.S: Before start, You have to undo pg_hba.conf file authenticate method to md5
Edit postgresql.conf and change password_encryption to
password_encryption = scram-sha-256
Restart Postgres service (or reload service)
reset the user password
# if use psql cli
\password <user>
# If use SQL command
alter user <user> with password '<password>';
After updating all passwords you should change pg_hba.conf authenticate method to scram-sha-256 and reset service again
Reference: Information about upgrade postgres password authenicate
I know that something that solved to me was in setting a new password (in CMD) when it was necessary to give a password for psql to be recognized:
set PGPASSWORD= #type here the password you want to set
After setting, you write the following (for example):
psql -h 188.81.81.92 -U postgres -d postgres -p 5432
where -h stands for host, -U for user, -d for the database root, -p for port, using the postgreSQL language.
In windows, it could happen to outcome a problem: error: connection to server (...), which can be solved by typing:
C:\\Program Files\\PostgreSQL\\14\\bin\\psql.exe
this is, you open the file psql.exe and it will work fine.

Change authentication method for postgres superuser

I am using psql to connect to a PostgreSQL database on Debian 10. I am trying to connect as the postgres user, to the default postgres database. By default, this is using the 'peer' authentication method, which does not require a password.
If I log in using the 'peer' authentication and set a password using the following command:
ALTER USER postgres WITH PASSWORD 'myPassword';
The query executes successfully, however when I edit pg_hba.conf to change the authentication method from:
local all postgres peer
to:
local all postgres scram-sha-256
and restart the server, I get the following error:
~$ sudo -u postgres psql postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
~$
Does anyone know how to do this?
To change the authentication method in PostgreSQL:
Open a terminal window
Change into the postgres bin directory
Example: cd /usr/local/pgsql/bin
Note: Depending on your install environment the path to the bin directory may vary.
Type su – postgres and press Enter. This will change the logged in to the postgres user.
From the bin directory type ./psql
Type:
ALTER USER your_username password 'new_password'; and press Enter. ALTER ROLE should be displayed.
Type \q and press Enter
Open /path_to_data_directory/pg_hba.conf
Example: /etc/postgresql/11/main/pg_hba.conf
Modify the line at the bottom of the config file to resemble one of these examples.
Note: You will probably only have to change the word trust to md5. The line or lines should already exist.
host all postgres peer
host all your_username your.ip your.subnet md5
Save the changes
Restart PostgreSQL service with systemctl restart postgresql.service
Before you assign the password, you probably need to set the password_encryption to "scram-sha-256". Otherwise, you stored the password in the md5 format, and such a password cannot be used to login when pg_hba.conf calls for "scram-sha-256".
The default setting of password_encryption is still md5. It will change to be "scram-sha-256" in v14.
The error message sent to the unauthenticated user is intentionally vague. The error message in the server log file will probably say DETAIL: User "postgres" does not have a valid SCRAM secret. (If it does not, then ignore this answer, and edit your question to tell us what it does say)
You need to 1st in the shell change to be the "postgres" user which you're not doing correctly above:
sudo su - postgres
Then you can do the following as peer auth:
psql -d postgres -U postgres
Also recommend you set a pw for postgres sql user:
\password postgres
& change the authentication method to "md5", not "peer".

How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

I'm having trouble using PostgreSQL. I have recently installed this version (13+223.pgdg20.04+1) of postgresql package in ubuntu 20.04.
I'm trying to run psql command, but I get the following error:
psql: error: FATAL: role "my_username" does not exist
I have tried to create a new user with createuser me, but I get the following error:
createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist
I have tried also forcing the postgres user with createuser me --username=postgres, but I get the following error:
createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"
How do I solve these problems to use PostgreSQL locally on my computer without these problems?
PD: I have reinstalled postgres and now I'm getting a different error while doing psql:
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I'm not sure why I had a bad installation, but I have completely uninstalled postgres following this post:
https://kb.objectrocket.com/postgresql/how-to-completely-uninstall-postgresql-757
after that I have restarted my computer and installed posgres again following the proper instructions in:
https://www.postgresql.org/download/linux/ubuntu/
and now it looks like it works without problems
Peer authentication means (there are advanced possibilities, but those are not going to be used by default, while the simple method is the default for apt-installed PostgreSQL) that you have to be the OS user 'postgres' to connect as the database user 'postgres'. So you would do:
sudo -u postgres createuser me
You don't need to specify --username=postgres, since that is the default behavior anyway once you use sudo -u postgres
Alternatively, you could change your pg_hba.conf to use a different authentication method other than peer, if you want to.
You need to provide username in the psql command using -U option.
psql -U postgres
Postgresql comes with a predefined superuser role called postgres. If you want to create more roles, you first have to connect as this initial role.
first check user postgres exists:
$ id postgres
Then:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.
If, Password for user postgres is no known then change it:
$ su - postgres
Password:
su: Authentication failure
$ sudo passwd postgres
New password:
Retype new password:
passwd: password updated successfully
Finally again:
$ su - postgres
Password:
$ psql
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

How to find or reset Postgresql psql postgres user password?

I am trying to get into my postgres shell to manage my database and I am running into an issue getting into my postgres shell.
omars-mbp:postgres omarjandali$ brew services restart postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
omars-mbp:postgres omarjandali$ psql
Password for user omarjandali:
psql: error: could not connect to server: FATAL: password authentication failed for user "omarjandali"
I also tried the default psql postgres user:
omars-mbp:postgres omarjandali$ psql -U postgres
Password for user postgres:
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
omars-mbp:postgres omarjandali$
Is there a way for me to reset the password. I don't remember setting a master password.
Step 1. Backup the pg_hba.conf file by copying it to a different location or just rename it to pg_hba.conf.bk
Step 2. Edit the pg_dba.conf file and change all local connections from md5 to trust. By doing this, you can log in to the PostgreSQL database server without using a password.
Step 3. Restart the PostgreSQL server (Service).
Step 4. Connect to PostgreSQL database server using any tool such as psql or pgAdmin:
psql -U postgres
PostgreSQL will not require a password to login.
Step 5. Execute the following command to set a new password for the postgres user.
ALTER USER postgres WITH PASSWORD 'new_password';
Courtesy of PostgreSQLTutorial

psql: FATAL: password authentication failed for user "akhil"

I have installed PostgreSQL 10.0 on my Windows 10, during the installation it asked me to enter a password for root user and I have created one. I wanted to run it over the command line and have set environment variables, but when i try to launch it by typing
C:\Users\akhil\Documents\Flask Examples\postsql>psql sample.sql
Password:
psql: FATAL: password authentication failed for user "akhil"
I've seen some solutions to change the pg_hba.conf file but It couln't help me.
(update)
I've also tried to create a user id but it shows the same error
createuser akhil
Password:
createuser: could not connect to database postgres: FATAL: password authentication failed for user "akhil"
This should help you get in to your database.
psql postgres://username:password#ip_address/database < sample.sql