Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: authentication fails when try to login with different unix user

I have a user hduser and user postgres,

with hduser:

hduser@master:/$ psql -Upostgres -W analytics
Password for user postgres: 
psql: FATAL:  Ident authentication failed for user "postgres"
hduser@master:/$ 

with postgres:

postgres@master:/opt/pentaho/biserver-ce$ psql -Upostgres -W analytics
Password for user postgres: 
psql (8.4.9)
Type "help" for help.

analytics=# 

How can I login to same database with a different user like hduser or some other?

Thank you

like image 808
daydreamer Avatar asked Dec 07 '11 18:12

daydreamer


People also ask

How do I fix Postgres password authentication failed?

Restart the PostgreSQL service from the Services control panel ( start->run->services. msc ) Connect using psql or pgAdmin4 or whatever you prefer. Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'

What is peer and MD5?

peer means it will trust the identity (authenticity) of UNIX user. So not asking for a password. md5 means it will always ask for a password, and validate it after hashing with MD5 . trust means it will never ask for a password, and always trust any connection.

How do I login as another user in PostgreSQL Ubuntu?

There are two ways to login PostgreSQL: By running the "psql" command as a UNIX user which is also configured as PostgreSQL user using so-called IDENT/PEER authentication, e.g., " sudo -u postgres psql ". Via TCP/IP connection using PostgreSQL's own managed username/password (using so-called MD5 authentication).


2 Answers

The default configuration of PostgreSQL allows "local" access to a database only to database users which have the same name as the OS user. "local" means without any IP traffic using only the Unix-Domain sockets. See the documentation for the configuration file pg_hba.conf especially the lines starting with "local" and the authentication method "peer" or "ident".

On the other hand accessing postgres using an IP transport channels is by default configured to use passwords IF there IS a password. Therefore this should help youfor normal users.

foouser@host$ psql -U baruser -h 127.0.0.1 database

The bad message is, that the DB superuser postgres does NOT have a password by default, so you must set one first.

like image 103
A.H. Avatar answered Nov 15 '22 10:11

A.H.


Postgres handles authentication and authorization separately.

Authentication options are configured in the file: pg_hba.conf -- this file describes what authentication methods users are allowed to use, and which hosts they can connect from.

Authorization to access databases and tables is configured by issuing GRANT statements in SQL.

On most Linux systems, the Postgres user is setup to use 'ident' authentication, and does not have a password by default, so if you wish to login using a password, you'll need to configure that with a SQL statement, and then alter your pg_hba.conf to allow the user postgres to login using a password:

ALTER ROLE postgres WITH PASSWORD password

like image 38
limscoder Avatar answered Nov 15 '22 11:11

limscoder