How to find or reset Postgresql psql postgres user password? - postgresql

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

Related

Unable to execute postgres command

I'm having difficulty changing the password associated with the postgres user after installing postgres on my windows 10 machine. My apologies in advance as I'm quite unfamiliar with postgres as well as the commands required to work with it.
I've referenced the approved answer in the below article:
FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)
I'm stuck on the step that requires me to
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
I don't quite understand this step. I've taken the following steps
Open cmd
run psql
The system then asks me for password for username jason. Regardless of what I enter, i get the following message:
psql: error: could not connect to server: FATAL: password authentication failed for user "jason"
At no point do I have an opportunity to enter the following command:
ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
How can I run this command without being asked to enter a password for postgres?
Thanks!
The steps below require that you remember what you did when you installed PostgreSQL.
Locate the data directory where the installation process created the database cluster. By default, that would be a subdirectory of where you installed the software (which is a bad place)
Edit the pg_hba.conf file therein and add this line on top:
host postgres postgres 127.0.0.1/32 trust
Reload or restart PostgreSQL.
Start cmd.exe and enter
psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
If psql is not on your PATH, use the absolute path C:\...\psql.
Use \password to change the password.

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.

Can't access psql due to authentication fail

I'm trying a tutorial for django that it uses postgresql but I have some issues in setting up the DB. I did change postgres password using sudo passwd postgres and I can login to postgres account using su postgres or sudo su - postgres but after that I can't access the postgres prompt with pqsl. It gives me the following error:
psql: FATAL: password authentication failed for user "postgres"
I've changed the pg_hba.conf file too (from peer to md5) but it didn't change anything.
I've never worked with postgresql and this is my first time using it so if you need any other information please ask me.
sudo passwd postgres is for the system user postgres which is why you can su to system user postgres shell. When you are doing psql -U postgres you are logging in as database user postgres. That is a different account. It is convention that the system user the Postgres server runs as is generally called postgres. Also by convention the Postgres server database 'root/superuser' is the name of the user that the server runs as, so again generally postgres. If you want to log in as postgres user to server using password you will need to create a password for the database user postgres. To do that I would see if in pg_hba.conf the local (not localhost) line is set to trust. If not set it to that and and do:
psql -U postgres -d postgres
Do not specify a -h. This will connect you via a local socket. Then you can :
https://www.postgresql.org/docs/12/sql-alteruser.html
ALTER USER postgres WITH PASSWORD 'your_password'
This will create a password for postgres user.
FYI, you don't have to log into system user postgres account to work as postgres user in database. All you have to do is specify -U postgres to any of the Postgres client programs, psql, pg_dump, etc. This also means you can work as postgres database user on remote servers.

AWS EC2 Postgres authentication failed

I did setup Postgres-12 on Centos AMI EC2, It works well.
However, Sometimes (my case is the next day) I can not connect my DB by trying
$psql -h XXXX -p5432 -Upostgres
A authentication failed error
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
Next, I change my postgres user again by
$sudo su - postgres
~]$ psql -c "alter user postgres with password 'postgres'"
The connection login become work.
But sometimes it become not work again (same above authentication failed)
I reset postgres password the same above command => The connection login become work.
It repeats #2
I don't know why. Does anybody know that? What should I do?
I realize that the my Postgres use very much CPU (%CPU: 99%).
hat could be solution to this problem. I would appreciate any suggestions.
Thank you

Phoenix and Postgres install - not talking

My OS is Fedora 26
I have installed Postgresql and Phoenix.
Postgres has a superuser "postgres" with password "postgres". This is confirmed by running \du in psql.
When I run $ mix ecto.create, I get
** (Mix) The database for Hello.Repo couldn't be created: FATAL 28000 (invalid_authorization_specification): Ident authentication failed for user "postgres"
I suspect it may be a permissions issue. To log into psql requires
$ sudo -u postgres psql postgres
Whereas Phoenix when attempting to use postgres may not have sudo privilages.
$ psql --version
psql (PostgreSQL) 9.6.8
Any thoughts appreciated.
By default the authentication for the postgres database user connecting to the DB locally is to verify that the operating system user is also postgres. This is what the error message refers to as Ident authentication and is why connection after doing sudo -u postgres works.
To connect as the postgres user using another means of authentication you need to edit the pg_hba.conf file. (HBA stands for host based authentication).
The line that allows this will look like this:
local all postgres peer
Add a line that looks like this (without removing the other line!):
local all postgres md5
And you should be able to connect using the password for postgres as well.
If I remember correctly you will need to restart the DB for this to take effect.