Changing password of postgres user led imidiate prompt of wrong password without providing a password - postgresql

So I forgot the password of the postgres user on my PostgreSQL server. I then changed all the md5 settings in the pg_hba.conf file to trust, restarted the server and then changed the password of the postgres user using ALTER USER postgres.... Now I changed the trust settings back to md5, restarted the server again, but when I now want to perform psql -U postgres it immediately returns an error:
psql: FATAL: password authentication failed for user "postgres"
password retrieved from file "/root/.pgpass"
without asking me to enter any password. The .pgpass file has the following line in it:
*:*:*:postgres:SOME_HASH
EDIT:
Somehow this only appears when logging in as root to my server... With my other user it works just fine...

The immediate solution is to remove the .pgpass file that contains the wrong password. If you didn't create that file yourself, odds are that pgAdmin did that for you when you told it to save the password.

If you specify '-W', then psql will prompt you for a password and ignore the .pgpass file. But why not fix .pgpass, or if you aren't using it, then remove it?

Related

cannot connect to Postgres - password unknown

I can't connect on postgresql.
when i go on pgadmin>server>postgresql, it asked me "please enter the password for the user 'postgres' to connect the server?
I have windows 10 and postgresql 14.
But I don't have this password.
What can I do?
The following worked for me.
Change METHOD of all rows in your pg_hba.conf file from scram-sha-256 to trust (C:\Program Files\PostgreSQL\14\data\pg_hba.conf). This will disable password for your DB.
Click on any database in postgresql to use Query Tool.
In Query Tool type ALTER USER postgres WITH PASSWORD 'User_password';
postgres is default username, execute it.
This way you can set up a password for your DB, after that go back to the first step and change it back from trust to scram-sha-256.

Fatal: password authentication failed for user "postgres" macos

I'm using PostgreSQL for the first time and I am not familiar with it. When I start pgadmin and enter master password and when I want to connect to postresql and again enter same password following error pops up:
could not connect to server: could not initiate GSSAPI security context: The operation or option is not available
could not initiate GSSAPI security context: Credential for asked mech-type mech not found in the credential handle
FATAL: password authentication failed for user "postgres"
Whats the problem here?
I just had the same issue and below is how I solved it. Because you don't have a default password for the user 'postgres', you will need to set one first, like this :
Connect to the terminal as the user 'postgres' with the current admin credential (thanks sudo)
sudo -u postgres -i
Connect to the database 'postgres'
psql postgres
(optional) If the psql command is not found, you can the PostgreSQL bin path to your $PATH like this :
export PATH=$PATH:THE_PATH_TO_POSTGRESQL_BIN_FOLDER
# Example : export PATH=$PATH:/Library/PostgreSQL/12/bin
Change the password of the user 'postgres'
--- Do not forget to my_password with the desired password
ALTER USER postgres PASSWORD 'my_password';
In the pgAdmin windows, try to connect with this new password, it should work.
It is ridiculous but my solution after many trial was very very simple.
Problem seems that password is not match or connection problem. So, first make sure your password is definitely correct. You might think you enter exact same but it might be not. I tried to wrote a password to somewhere else that i can see it is correct.Then, i copied that and paste it to setup where you put your new password. Ofcourse you have to remove postgre completely for this to try.
When setup is completed, if you tried and didn't work again, then delete Postgre server and create new one. Just enter server name, connection address and password and save. if this one also not work successfully,maybe you should check if the chosen port number is alive or not for postgre server.
Sorry for that answer is not visual and some english mistakes. I just wanted to wrote really fast. Good luck :)

Why Is Knex.js Failing on Password Authentication When My Database Has No Password?

I'm trying to connect to my database using Knex like so:
const client = knex({
client: 'postgresql',
connection: {
host: '127.0.0.1',
user: 'me',
database: 'my_db'
}
});
client('some_table').then(console.log);
When I created the database (using createdb my_db at the command line) I set no password. At the command line I can run psql -d my_db and it works just fine.
However, when I try to use Knex, I get an error:
Unhandled rejection error: password authentication failed for user "me"
This happens whether I set a null password, an empty string ('') password, or leave the field off of the configuration entirely.
Can anyone explain why Knex insists on failing password authentication ... when there is no password (and when I can connect without one at the command line just fine)?
PostgreSQL does not permit login based on the presence/absence of a password. Rather, all login authentication is handled via pg_hba.conf. In your particular case--creating the me user without a password (or using null or '', as you would describe it)--the absence of the password doesn't necessarily allow your user to log in. In fact, setting no password will not allow that user to log in unless pg_hba.conf was set to some password-less setting (i.e., peer or trust).
If you want password-less login for the me user (I assume for testing/development purposes, as having password-less login is not good security practice in production), you could simply set trust-level authentication in pg_hba.conf:
#conn_origin database user ip_mask auth_method
host all me 0.0.0.0/0 trust
The more secure method of implementing password-less authentication would be to use a .pgpass file or set the PGPASSWORD environment variable. Seeing that you are trying to use knex.js, you may be better off with tweaking pg_hba.conf as above. Again, I don't know what your intentions are, but please be safe out there ;)
Disclosure: I work for EnterpriseDB (EDB)

How to provide password in one script to connect to postgresql database

I want to backup my postgresql database with a automatic backup program and that program only support a script line that backup with pg_dump.
I can't provide password in this script so I changed method from pg_hba.conf file from md5 to trust so that it won't ask for password and I used --no-password option.
How can I change this to md5 and provide password for it.
I would use a .pgpass file or use the peer authentication method. Details here:
Run batch file with psql command without password

Starting pgAdmin III with stored passwords asks for password again

Starting pgAdmin III on kubuntu with stored password asks for password every time connecting to database giving error "Error connecting to the server: fe_sendauth: no password supplied". It ignores checkbox in store password field.
I found reason for this in Postgresql mailing lists. Problem is when file /home/user/.pgpass has permisions more than u=rw 0600 pgAdmin ignores the file. Changing permisions for file resolves problem.