There are many cases where external connection poolers like pgBouncer become unavoidable despite the costs and complexities associated with them. PgBouncer is one of the most popular external connection poolers for PostgreSQL. It is thin and lightweight, so it doesn’t have built-in authentication features like LDAP, which is essential for many enterprises. Luckily, pgBouncer has support for PAM. So, a setup of PAM-LDAP could be the right solution when pgBouncer is used. When it comes to LDAP / AD, the authentication happens outside PostgreSQL. So, generally, DBAs don’t have much control over it other than doing some final configuration. This blog post is about the final configuration required, and setting up an LDAP server is not covered.

Test setup

For this write-up, I am using a simple OpenLDAP – slapd setup, which comes along with most Linux Distros, including Ubuntu. How to setup an OpenLDAP server is beyond the scope of this blog. The LDAP / Active Directory for your organization could be different. The configuration of PostgreSQL and pgBouncer remains independent of the LDAP server used. In case you are interested in setting up SAMBA as your LDAP server, please refer to the blog post Testing LDAP Authentication and Authorization on Percona Operator for MongoDB.

In OpenLDAP-slapd, I have a LDAP user account jobin with the following details.

This account is added to the directory using ldapadd utility.

The password for the account is set as jobin123” using ldappasswd:

A user with the same name should exist in the database because LDAP-PAM is only for authentication, because Authorization and Access control require real database users.

Please note that the password on the database side is kept different from the LDAP password to make sure that authentication is really happening with the LDAP password, not the password of the user account in the database.

PostgreSQL can handle LDAP authentication directly without PAM. Here is the sample pg_hba.conf entry:

We shall verify whether the LDAP authentication is working fine for PostgreSQL.

Example with wrong LDAP password:

Example with correct LDAP password:

Installing and configuring PAM

We can check for available PAM modules in the system. We are interested in pam_ldap:

If the module is not already present, we may have to install it:

This will automatically start the configuration.

Here are the sample configuration entries:

Generally, the “common-auth” configuration would be sufficient:

However, many users prefer to have a dedicated configuration for pgBouncer, and pgBouncer, by default, gives the service name “pgbouncer.” If you prefer that route, you may create a configuration file /etc/pam.d/pgbouncer:

Testing PAM authentication

It is a good idea to test whether the PAM authentication is working before proceeding to configure the pgBouncer for PAM. Here, I am using the tool pamtester, which is available for most Linux Distros.

Verify that the PAM authentication happens with the correct password and fails otherwise.

pgBouncer configuration

As a first step, please make sure that the pgBouncer is compiled with pam support.

As we can see, the pgBouncer is linking to libpam.so.0. Those who compile pgBouncer from the source often miss this, and it is one of the most frequent causes of trouble.

Now we can proceed to configure the pgBouncer. For this demo, I have a very simple pgBouncer configuration as follows:

Now, once we start the pgBouncer service, we should be able to authenticate using pam-ldap:

An authentication failure will produce errors as follows in pgBouncer logs.

Note:

  1. This blog post demonstrates minimal functionality. It does not recommend configurations for production systems.
  2. This blog post doesn’t recommend external connection poolers everywhere. External connection poolers like pgBouncer need to be considered only in those cases where the application doesn’t have a good connection pooler or wherever it is unavoidable. The use should be judiciously controlled.

 

Our PostgreSQL Performance Tuning eBook condenses years of database expertise into a practical guide for optimizing your PostgreSQL databases. Inside, you’ll discover our most effective PostgreSQL performance strategies derived from real-world experience.

 

Download now: Elevate your PostgreSQL Performance

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments