4 types of postgresql user authentication methods you must know

In this tutorial, we will learn everything about PostgreSQL user authentication in the PostgreSQL server.

How the PostgreSQL user authentication is done when you login to the database?

By default, when PostgreSQL server is installed, a user called Postgres and a database called Postgres is created.

There will be two more databases called template0 and template1 are created by default, but we limit the post with user authentication only.

If you want to login to the Postgres database with postgres user we simply query psql.

But, what is the default password for Postgres user?

The answer is simple, there isn’t be any default password for Postgres user.

The first thing we need to do after the database is created is to set the password for the Postgres user.

How to Change a Password for PostgreSQL user?

use the following command to change/set the password for your current user

\password

But the question is how did it login without prompting for the password or even without setting a password?

The reason why it didn’t ask for a password is its authentication method.

The authentication method configuration will be there in pg_hba.conf file under the data directory.

The default authentication method for PostgreSQL server is either be ident or peer

There are two more authentication methods which are widely used are trust and md5

Let’s understand each of them in detail.

PostgreSQL User Authentication types:

Peer Authentication:

Obtain the client’s operating system user name from the operating system and check if it matches the requested database user name. This is only available for local connections.

Here, as the database username and the OS username are same, the peer authentication method used OS credentials and logged in successfully.

If I use a database user other than Postgres it throws an error.

psql: FATAL: Peer authentication failed for user

Here, as testing user is not there at OS level my authentication failed.

How to resolve the issue psql: FATAL: Peer authentication failed for user

To resolve the above issue, we have to map the operating system user name to a database user.

User Name Mapping :

username mapping can be done in two steps.

STEP 1: Add a mapping configuration in pg_ident.conf file.

STEP 2: specify map=map-name in the options field in pg_hba.conf.

Reload the Postgres server

Trust Authentication

Allow the connection unconditionally. This method allows anyone that can connect to the PostgreSQL database server to login as any PostgreSQL user they wish, without the need for a password or any other authentication.

The following is the line mentioned in pg_hba.conf for local authentication

Here, irrespective of the password given, the postgres server allows the user to login.

md5 Authentication

Require the client to supply a double-MD5-hashed password for authentication.

The following is the line mentioned in pg_hba.conf for md5 authentication

How does it work?

Here, as I have not supplied any password while creating the user, my login failed.

After changing the password for the md5user I could log in to the database.

ident Authentication

Obtain the operating system user name of the client by contacting the ident server on the client and check if it matches the requested database user name. Ident authentication can only be used on TCP/IP connections. When specified for local connections, peer authentication will be used instead.

ident works similar to peer authentication.

More about ident authentication method can be found here.

Conclusion:

Depending on the environment needs, we either set trust or md5 as our default authentication method.

Peer and ident methods are more dependent on OS usernames, so we don’t use these authentication methods.

Do you use ident or peer authentication in your environment?

or

What authentication method you follow the most?

Please share your thoughts by commenting below.

 

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestion/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

 

 

 

 

 

This Post Has One Comment

  1. Ramanna Gunde

    Very Informative. Thank you!

Leave a Reply