Centos postgres connect - postgresql

When you try to connect to postgres:
# psql -h localhost dgrt postgres
I get an error:
#User "postgres" has not passed authentication (Ident)
Configuration my files
postgresql.conf:
listen_addresses = '*'
pg_hba.conf:
#Allow replication connections from localhost, by a user with the
#replication privilege.
host all all 127.0.0.1/32 ident

With ident auth you need to launch the command as the correct OS user, i.e., the database username must match the OS username on the local host.
For example (as root) you can do:
# su postgres -c 'psql dgrt'
The same applies to connections to remote hosts. For details, please refer to the documentation.
For reference:
https://www.postgresql.org/docs/9.3/static/auth-methods.html#AUTH-IDENT
https://www.postgresql.org/docs/9.3/static/auth-pg-hba-conf.html
http://man7.org/linux/man-pages/man1/su.1.html

Related

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres" (Ubuntu)

When I try to open psql with this command:
psql -U postgres
I get this error:
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"
But it connects successfully when I use:
sudo -u postgres psql
Can someone please explain what is happening and how to diagnose/fix this problem? My pg_hba.conf contains the following:
# 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 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
You can edit your .conf files with privileges using an editor, for my case it is nano.
$sudo nano /etc/postgresql/14/main/pg_ident.conf
Map your user by adding this line
# MAPNAME SYSTEM-USERNAME PG-USERNAME
user1 <computer-username> postgres
Replace the <computer-username> with the System-Username, which can be found using the whoami command. Type in your terminal:
$whoami
Then go ahead to the pg_hba.conf
$sudo nano /etc/postgresql/14/main/pg_hba.conf
Add your postgre user, with method=peer, as shown below:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
This worked for me.
Peer authentication means that the connection is only allowed if the name of the database user is the same as the name of the operating system user.
So if you run psql -U postgres as operating system user root or jimmy, it won't work.
You can specify a mapping between operating system users and database users in pg_ident.conf.
step-1
vi /etc/postgresql/14/main# vi postgresql.conf
listen_addresses = '*'
step-2
vi /etc/postgresql/14/main# vi pg_hba.conf
# 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 0.0.0.0/0 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
step-3
sudo /etc/init.d/postgresql restart
step-4
After restart your changes
create your required database and database username
I don't think in production you wana bother yourself changing any files.
So, What you can do is temporarily make the username same to the username of your postgres which is usually postgres by passing command sudo -i -u postgres and then psql, now you successfully connected to postgres.

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.