How to force using password - postgresql

I have just installed PostgreSQL on my Mac After the installation I logged in via:
$ psql postgres
At the next step I created new database:
CREATE DATABASE sample_db;
And now I added a user and grant it access to created DB:
CREATE USER sample_user WITH PASSWORD 'sample_pass';
GRANT ALL PRIVILEGES ON DATABASE sample_db TO sample_user;
When I log in with user, I do not need to enter the password:
psql sample_db -U sample_user
even, if I provide the -W option.
Also, if I try to log in with this user via some SQL-GUI such a tableplus.
How can I configure postgresql to force user using their password?
I modified the pg_hba.conf file to looking like this:
host sample_db sample_user 127.0.0.1/32 password
My intention or my guess was that psql will use it. But somehow it doesn't.
I also checked How to Force Postgresql User Login with Password, but with no positive effect to my issue.
pg_hba.conf
local all admin trust
local all all md5
host all admin 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
host all admin ::1/128 trust
host all all ::1/128 md5

I have a solution
The thing is that while you installing postgresql via brew there will be two pathes, where pg_hba.conf can potentially exist.
Wrong path: /opt/homebrew/opt/postgresql#15/share/postgresql#15/pg_hba.conf
Screen: inactive pg_hba path
Right path: /opt/homebrew/var/postgresql#15/pg_hba.conf
Screen: active pg_hba path
You can find the path of your pg_hba.conf inside pg_settings table. Just run
select * from pg_settings;
-
select setting from pg_settings where name = 'hba_file';
After placement of needed configuration and postgresql server restart, everything works like expected.

Related

Postgresql - Password authentication failed [duplicate]

I'm a noob in PostgreSQL. I installed ver 9.2 on windows7. During installation it asked for password and i entered it.
Now whenever i run d:\tools\PostgreSQL9.2\bin\psql.exe it asks for password. When i enter the password it doesn't accept and
it shows "password authentication failed for user "user1". I have re-installed twice already. Also i tried entering my system
password.
I'm trying to get the below command to work
psql.exe -f db/codedb.sql development
What should i do to get this working ?
Try setting the user name when connecting.
psql.exe -U username -d dbname -f somefile.sql
You've probably set up the default "postgres" user during installation. Not sure if you've created any others.
To add other users and databases just connect to as postgres to the postgres database and do something like:
CREATE USER myuser WITH ENCRYPTED PASSWORD 'secret';
CREATE DATABASE mydb OWNER myuser;
If your machine is secure you might also like to set up a password file
change "trust" instead of "md5" in the pg_hba.conf to connect to the database and change your password.
--------------------configuration in pg_hba.conf---------------
local all all trust
local all postgres trust
host all all ::1/128 trust
Here is the simple solution for installation Postgresql without getting errors(cluster errors and authentication errors),i have followed below steps and i got installed postgresql sucessfully
create new user in windows from controlpanel-->user accounts
After logged into new user(whic u hve created) copy postrgresql(.exe) application into any directory(other than 'C') and click on the application to install(dont forget to change the installation directory to which u have copied the application file above).
after completion of installaion change below configurations in postgresql.conf and pg_hba.cof
add like below in your postgresql.conf
listen_addresses = '*'
add like below in your pg_hba.cof
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
# host sameuser postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 md5

Cannot connect to local postgresql DB using DBeaver

I'm trying to connect to a postgresql database which is in localhost:5432 but I keep getting the error:
FATAL: Ident authentication failed for user "".
I installed Postgres11 on virtual machine running Centos7. Created a database through command line, with the name business_db.
I've checked and postgresql is running in localhost:5432.
My pg_hba.conf file is like this:
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
The pg_ident.conf file doesn't hold any configurations:
# Put your actual configuration here
# ----------------------------------
# MAPNAME SYSTEM-USERNAME PG-USERNAME
The database exists as shown by the command:
I'm logged into the system as "dev" user but., whatever I try while testing connection whit DBeaver, I allways get the error:
I also tried to set User as postgres and use my system password but get the same error. What am I missing?
When you use JDBC, you have to use password authentication. Neither ident nor peer will work for that.
You will need to add, e.g.:
host all all 127.0.0.1/32 md5
at the top of your pb_hba.conf
(replace md5 with scram-sha-256 if you are using that)
Change the ident to trust if you don't want to enter a password or to md5 if you want to enter a password.
Then reload the PostgreSQL server.
In (Ubuntu OS), change (or set) password in this way to solve this problem:
1- In terminal, run this command:
sudo -u postgres psql template1
2- Then run this command:
ALTER USER postgres with encrypted password 'your_password';
remove any spaces from your password, this worked for me

Postgres server password reset not working

I have Postgresql server 8.2 running on a Windows server. I am trying to reset the root postgres account password and just not having any luck. I have so far done the following:
Edited pg_hba.conf to allow:
local all all trust
I then restarted the postgres server so changes could be applied.
Then I opened command prompt and change directory to the bin folder of the postgres installation folder that has all the postgres .exe files.
From what I understand I am supposed to type the following:
psql -U postgres
And at this point it should let me in and should type:
ALTER USER postgres with password 'newpassword';
However, it keeps prompting me for password for user postgres:
And it just does not seem to be working. So from my understanding it seems that the the trust local rule in the pg_hba.conf is not really working correctly.
Update
My pg_hba.conf file only has the following uncommented tags in it:
host all all 127.0.0.1/32 trust
local all all trust
host all all ::1/128 md5
You seem to be asking to change the user on the operating system password. The Postgres user is a user on the the operating system.
Plus you are locked out due to the security hierarchy in Host Based Authentication (HBA)
host all all 127.0.0.1/32 trust
local all all trust
-> host all all ::1/128 md5 <-
NB Loopback Address - ::1/128
::1/128 is the loopback address of the local host which is the equivalent of the 127.0.0.1 in IPv4.
If the postgres user password (as in operating system user) is not set, the postgresq user password is stored
%APPDATA%\postgresql\pgpass.conf
Ref Location of postgresql default user password if not set
On Unix systems, the permissions on .pgpass must disallow any access to world or group; achieve this by the command chmod 0600 ~/.pgpass. If the permissions are less strict than this, the file will be ignored. On Microsoft Windows, it is assumed that the file is stored in a directory that is secure, so no special permissions check is made.
You could update you Host Based Authentication (HBA) file and if you wanted to; change the password for the postgres user on the operating system or windows Then everything should be fine from psql
Hope this helps

psql.exe - password authentication failed in windows

I'm a noob in PostgreSQL. I installed ver 9.2 on windows7. During installation it asked for password and i entered it.
Now whenever i run d:\tools\PostgreSQL9.2\bin\psql.exe it asks for password. When i enter the password it doesn't accept and
it shows "password authentication failed for user "user1". I have re-installed twice already. Also i tried entering my system
password.
I'm trying to get the below command to work
psql.exe -f db/codedb.sql development
What should i do to get this working ?
Try setting the user name when connecting.
psql.exe -U username -d dbname -f somefile.sql
You've probably set up the default "postgres" user during installation. Not sure if you've created any others.
To add other users and databases just connect to as postgres to the postgres database and do something like:
CREATE USER myuser WITH ENCRYPTED PASSWORD 'secret';
CREATE DATABASE mydb OWNER myuser;
If your machine is secure you might also like to set up a password file
change "trust" instead of "md5" in the pg_hba.conf to connect to the database and change your password.
--------------------configuration in pg_hba.conf---------------
local all all trust
local all postgres trust
host all all ::1/128 trust
Here is the simple solution for installation Postgresql without getting errors(cluster errors and authentication errors),i have followed below steps and i got installed postgresql sucessfully
create new user in windows from controlpanel-->user accounts
After logged into new user(whic u hve created) copy postrgresql(.exe) application into any directory(other than 'C') and click on the application to install(dont forget to change the installation directory to which u have copied the application file above).
after completion of installaion change below configurations in postgresql.conf and pg_hba.cof
add like below in your postgresql.conf
listen_addresses = '*'
add like below in your pg_hba.cof
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
# host sameuser postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 md5

I forgot the password I entered during PostgreSQL installation

I either forgot or mistyped (during the installation) the password to the default user of PostgreSQL. I can't seem to be able to run it, and I get the following error:
psql: FATAL: password authentication failed for user "hisham"
hisham-agil: hisham$ psql
Is there a way to reset the password or how do I create a new user with superuser privileges?
I am new to PostgreSQL and just installed it for the first time. I am trying to use it with Ruby on Rails and I am running Mac OS X v10.7 (Lion).
Find the file pg_hba.conf. It may be located, for example, in /etc/postgresql-9.1/pg_hba.conf.
cd /etc/postgresql-9.1/
Back it up
cp pg_hba.conf pg_hba.conf-backup
Place the following line (as either the first uncommented line, or as the only one):
For all occurrence of below (local and host) , except replication
section if you don't have any it has to be changed as follow ,no MD5
or Peer authentication should be present.
local all all trust
Restart your PostgreSQL server (e.g., on Linux:)
sudo /etc/init.d/postgresql restart
If the service (daemon) doesn't start reporting in log file:
local connections are not supported by this build
you should change
local all all trust
to
host all all 127.0.0.1/32 trust
You can now connect as any user. Connect as the superuser postgres (note, the superuser name may be different in your installation. In some systems it is called pgsql, for example.)
psql -U postgres
or
psql -h 127.0.0.1 -U postgres
(note that with the first command you will not always be connected with local host)
Reset the password ('replace my_user_name with postgres since you are resetting the postgres user)
ALTER USER my_user_name with password 'my_secure_password';
Restore the old pg_hba.conf file as it is very dangerous to keep around
cp pg_hba.conf-backup pg_hba.conf
Restart the server, in order to run with the safe pg_hba.conf file
sudo /etc/init.d/postgresql restart
Further reading about that pg_hba file: 19.1. The pg_hba.conf File (official documentation)
When connecting to PostgreSQL from the command line, don't forget to add -h localhost as a command line parameter. If not, PostgreSQL will try to connect using PEER authentication mode.
The below shows a reset of the password, a failed login with PEER authentication and a successful login using a TCP connection.
# sudo -u postgres psql
could not change directory to "/root"
psql (9.1.11)
Type "help" for help.
postgres=# \password
Enter new password:
Enter it again:
postgres=# \q
Failing:
# psql -U postgres -W
Password for user postgres:
psql: FATAL: Peer authentication failed for user "postgres"
Working with -h localhost:
# psql -U postgres -W -h localhost
Password for user postgres:
psql (9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
postgres=#
The pg_hba.conf (C:\Program Files\PostgreSQL\9.3\data) file has changed since these answers were given. What worked for me, in Windows, was to open the file and change the METHOD from md5 to trust:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
Then, using pgAdmin III, I logged in without using a password and changed user postgres's password by going to menu File → Change Password.
I was just having this problem on Windows 10 and the issue in my case was that I was just running psql and it was defaulting to trying to log in with my Windows username ("Nathan"), but there was no PostgreSQL user with that name, and it wasn't telling me that.
So the solution was to run psql -U postgres rather than just psql, and then the password I entered at installation worked.
Edit the file /etc/postgresql/<version>/main/pg_hba.conf and find the following line:
local all postgres md5
Edit the line and change md5 at the end to trust and save the file
Reload the postgresql service
sudo service postgresql reload
This will load the configuration files. Now you can modify the postgres user by logging into the psql shell
psql -U postgres
Update the postgres user's password
alter user postgres with password 'secure-passwd-here';
Edit the file /etc/postgresql/<version>/main/pg_hba.conf and change trust back to md5 and save the file
Reload the postgresql service
sudo service postgresql reload
Verify that the password change is working
psql -U postgres -W
For Windows (what has helped me):
This is the document I am referring to: How can I reset a PostgreSQL password?
Open your cmd and go to C:\Program Files\PostgreSQL\12\data.
This is usually the right path. You might have it stored somewhere else. Note that, if you have a different PostgreSQL version, there will be a different number. That doesn't matter.
Find a pg_hba.conf file and copy it to somewhere else (that way you will have an unmodified version of this file, so you will be able to look at it after we make some changes)
Open the pg_hba.conf file (not the backup, but the original)
Find the multiple lines that start with host near the bottom of the file:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
Replace md5 with trust:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
Close this file
Go to your search bar on windows and open Services app. Find postgres and restart it.
Picture of services app
Write cd.. in cmd and then cd bin. Your path should be C:\Program Files\PostgreSQL\12\bin
Enter: psql -U postgres -h localhost
Enter: ALTER USER postgres with password '<your new password>';Make sure that you include ; at the end
“ALTER ROLE” should be displayed as an indication that the previous line was executed successfully
Open original pg_hba.conf file and change back from trust to md5
Restart the server with Services app as before
Just a note: On Linux, you can simply run sudo su - postgres to become the postgres user and from there change what is required using psql.
For a Windows user for the latest PostgreSQL version (greater than 10):
Go to your PostgreSQL installation location, and search for pg_hba.conf, you will find it in ..\postgres\data\pg_hba.conf.
Open that file with Notepad, and find this line:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
#..
Change the method from *md5* to *trust*:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# ...
Now go to your SQL shell (PSQL) and leave everything blank,
Server [localhost]:
Database [postgres]:
Port [8000]:
Username [postgres]:
It will not ask for a password this time, and you will be logged in,
Now run this line:
`ALTER USER yourusername WITH SUPERUSER`
Now you can leave the shell with \q.
Again, go to the file pg_hba.conf and change METHOD from trust to md5 again, and save it.
Now log in with your new user and password, and you can check \du for its attributes.
For a Windows installation, a Windows user is created. And "psql" uses this user for connection to the port. If you change the PostgreSQL user's password, it won't change the Windows one.
The command line just below works only if you have access to the command line.
Instead, you could use the Windows GUI application "c:\Windows\system32\lusrmgr.exe". This application manages users created by Windows. So you can now modify the password.
I did this to resolve the same problem:
Open the pg_hba.conf file with the gedit editor from the terminal:
sudo gedit /etc/postgresql/9.5/main/pg_hba.conf
It will ask for a password. Enter your admin login password.
This will open gedit with the file. Paste the following line:
host all all 127.0.0.1/32 trust
just below -
# Database administrative login by Unix domain socket
Save and close it.
Close the terminal, open it again and run this command:
psql -U postgres
You will now enter the psql console.
Now change the password by entering this:
ALTER USER [your preferred user name] with password '[desired password]';
If it says the user does not exist then instead of ALTER, use CREATE.
Lastly, remove that certain line you pasted in pg_hba and save it.
If you are running PostgreSQL on macOS, try these:
1. Edit the pg_hba.conf file
sudo vi /Library/PostgreSQL/9.2/data/pg_hba.conf
and Change the "md5" method for all users to "trust" near the bottom of the file
2. Find the name of the postgres service
ls /Library/LaunchDaemons
Look for postgresql
3. Restart the postgresql service
sudo launchctl stop com.edb.launchd.postgresql-9.2
sudo launchctl start com.edb.launchd.postgresql-9.2 (com.edb.launchd.postgresql-9.2 should be name postgresql service from step 3)
4. Start a psql session as postgres
psql -U postgres
(shouldn't ask for password because of 'trust' setting)
5. Reset password in the psql session by typing:
ALTER USER postgres with password 'secure-new-password';
6. Edit the pg_hba.conf file
Switch it back to 'md5'
8. Restart services again
If you are on Windows you can just run
net user postgres postgres
And log in in PostgreSQL with postgres/postgres as the user/password.
Follow step 1 on the best answer.
Here is my addition if you use the Windows operating system. Follow only step 1, and then open pgAdmin or postgres on web and click on file on the top nav. Click on reset layout, and finally reload the application. Whatever password you put should work. I used 1234.
I didn't manage to find the file pg_hba.conf in the folder C:\Program Files\PostgreSQL\14\data, because there is not a folder data at all.
I solved the problem by creating a new user using pgAdmin and gave it super system administrator rights.
Add the below line to your pg_hba.conf file. Which will be present in the installation directory of PostgreSQL
hostnossl all all 0.0.0.0/0 trust
It will start working.