Requiring password for logging into postgres db - postgresql

I'm trying to require all user to provide their password to access my postgres db.
Currently, if I run psql -U <username> <dbname> or psql --username=<username> <dbname> I can log in without the password being provided.
I've followed along several stackoverflow questions and answers, and none have fixed this issue.
Why isn't psql requiring me to enter a password to access psql for a login role I just created a password for?
How to Force Postgresql User Login with Password
Here is what my pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5

Related

How to Control PostgreSQL Password authentication both Front end and Command line in Windows 10 and Redhat (Linux)?

Recently I'm using PostgreSQL9.2 both in Windows 10 and Redhat (Linux).
But now I'm little concern about PostgreSQL access authentication.
Late me explain...
When i first installed PostgreSQL in my system i used two different password for two log in role postgres (default) and adempiere (created). In Redhat i use to psql -h localhost -U postgres db_name and psql -h localhost -U adempiere db_name command. Sometime this command wants password sometime not.
Recently I have used ALTER USER postgres WITH PASSWORD 'new_password'; and ALTER USER adempiere WITH PASSWORD 'new_password'; command to change. But here is an another problem. After changed my password when i get database access with psql -h localhost -U adempiere db_name command, i can access with both old_passwod and new_password.
Now what can i do for Strong authentication in my Database both Windows 10 and Redhat (linux) ?
Here is part of my pg_hba.conf for reference:
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records. In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.2.151/32 trust
host all all 192.168.2.5/32 trust
host all all 192.168.2.6/32 trust
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres md5
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
To force password authentication for every connection, replace trust with md5 everywhere in pg_hba.conf and reload the server.
It is dangerously negligent to use PostgreSQL 9.2. Use a later version. For one, this will offer the more secure scram-sha-256 hashing method for password authentication.

psql: FATAL: password authentication failed for user {user}

I'm pulling my hair out trying to figure out what is wrong.
I am attempting to connect to a local install of postgres 11 on ubuntu 19.
sudo -u postgres psql gives me full access to the psql shell as user postgres. I created a new user and db, granted permissions, set a password, edited the pg_hba.conf file, and restarted the server. I have also ensured that my password is not expired.
Now I expect psql "host=localhost user=marcos port=5433 dbname=reddit sslmode=require" to connect to my database, but I get a password error: "psql: FATAL: password authentication failed for user". I know the password I typed is correct, so what is wrong?
Here are the relevant pg_hba.conf lines:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
# custom rules
host all marcos all md5
follow this step for ubuntu/linxs
-> sudo -u postgres psql
-> postgres=#
After that right
->postgres=# \password
Enter new password for user “postgres”:
Enter it again:
Now check, the new password is working successfully.
Maybe something is wrong with your old password, try this:
ALTER ROLE marcos WITH ENCRYPTED PASSWORD '<new_password>';
And then connect to your DB with this password.

Not able to connect to postgres on ec2

I just installed PostgreSQL 9.2 server on an EC2 AMI instance. However I am not able to connect to it from the command prompt.
Moreover I see two directories in /var/lib: pgsql9 and pgsql92. The data directory in pgsql92 is empty and hence it looks like pgsql9 is the one that is getting used.
[root#ip-172-31-56-103 etc]# psql
Password:
psql: FATAL: password authentication failed for user "root"
[root#ip-172-31-56-103 etc]# sudo su - postgres
-bash-4.2$ psql
Password:
psql: FATAL: password authentication failed for user "postgres"
-bash-4.2$ psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
-bash-4.2$
pg_hba.conf
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 ident
#host replication postgres ::1/128 ident
postgresql.conf
listen_addresses = '*'
To work out which PG install you are using: ps -ef | grep pgsql. You will see for sure which binary, data directory and conf file is being used to give you some comfort.
Have you changed the pg_hba.conf from the default? If so, did you reload it? Something like sudo service postgresql reload should do it, depending upon your OS.
You might want to change the IPv6 local connection to use md5 as well.
Try adding -h localhost or -h 127.0.0.1 to your psql command: e.g. psql -h localhost -U postgres.
Check your postgres password to be doubly / triply sure.
Otherwise, check out the specific docs for your OSs installation. Sometimes apt or yum repos do some additional security configuration for you.
Finally, worst case, change all the pg_hba.conf auth methods to trust, then restart the database, logon, change the postgres password, logout, change the auth methods to md5, reload and try to logon again.

How to Force Postgresql User Login with Password

All I did is in my local machine(Mac OS).
After installing the postgresql, I created a user named poet with password, then created a database named poems and the database's owner is poet.
What I am curious is that I can log into poems with poet without password.
The command is psql -U poet -d poems.
Then what's the usage of the password?
I know that I can add -W to get the password prompt psql -U poet -d poems -W, but I can still log in even with a wrong password!
Then I modify the config file(/usr/local/var/postgres/pg_hba.conf) like below:
# "local" is for Unix domain socket connections only
#local all all trust
local all all password
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all 127.0.0.1/32 password
# IPv6 local connections:
#host all all ::1/128 trust
host all all ::1/128 password
And restart the server, I still don't need to provide the password to log into the database.
Can someone tell how to force the server to verify the correctness of login password?
This was happening to me as well. Non privileged accounts were able to login with invalid passwords. The steps I took to resolve it were.
Find the right pg_hba.conf file.
$ psql -h 127.0.0.1 -U admin -W postgres
Password for user admin:
psql (9.6.5)
Type "help" for help.
openproject_dev=# SHOW hba_file ;
hba_file
-------------------------------------
/usr/local/var/postgres/pg_hba.conf
(1 row)
Edit the file to force password login ... Leave admin (superuser) as trust.
I used md5 instead of password .. password worked as well.
# "local" is for Unix domain socket connections only
local all admin trust
local all all md5
# IPv4 local connections:
host all admin 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all admin ::1/128 trust
host all all ::1/128 md5
Save pg_hba.conf file and restart the postgres server.
Maybe it will work for you to create the database's user and schema in PostgreSQL:
Log into PostgreSQL from the postgres user
$ sudo -u postgres psql postgres
Once in, create the user and database
CREATE ROLE myuser LOGIN PASSWORD 'mypass';
CREATE DATABASE mydatabase WITH OWNER = myuser;
Log into PostgreSQL from the new user account
$ psql -h localhost -d mydatabase -U myuser -p <port>

psql: FATAL: Ident authentication failed for user "postgres"

I have setup a Ubuntu VPS as per the railcast episode #335. I've worked through a few issues but can't get past the error above when starting Unicorn. I have been testing with this command to connect to the db since.
psql -U postgres -W
I have edited /etc/postgresql/8.4/main/pg_hba.conf as follows
# Database administrative login by Unix domain socket
local all postgres trust
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
It doesn't seem to matter what METHOD I use (trust, md5, password) the error is the same.
Any ideas would be appreciated.
Try using psql -U postgres -h localhost
As Per my setting plz use below setting can help this setting dear...
host all all 0.0.0.0/0 md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You usually connect as the postgres user with sudo -u postgres psql. Note that sudo prompts for the current users password (unless you are root), not a user from the database.