bitnami-containers/bitnami/pgbouncer
Alexander Bayandin 7e5b373d4c
[bitnami/pgbouncer] Add auth_dbname to available environment variables (#52264)
Signed-off-by: Alexander Bayandin <alexander@neon.tech>
2023-12-21 12:14:46 +01:00
..
1/debian-11 [bitnami/pgbouncer] Add auth_dbname to available environment variables (#52264) 2023-12-21 12:14:46 +01:00
README.md [bitnami/pgbouncer] Add auth_dbname to available environment variables (#52264) 2023-12-21 12:14:46 +01:00
docker-compose-password.yml
docker-compose.yml

README.md

Bitnami package for PgBouncer

What is PgBouncer?

PgBouncer is a connection pooler for PostgreSQL. It reduces performance overhead by rotating client connections to PostgreSQL databases. It supports PostgreSQL databases located on different hosts.

Overview of PgBouncer Trademarks: This software listing is packaged by Bitnami. The respective trademarks mentioned in the offering are owned by the respective companies, and use of them does not imply any affiliation or endorsement.

TL;DR

docker run --name pgbouncer bitnami/pgbouncer:latest

Docker Compose

curl -sSL https://raw.githubusercontent.com/bitnami/containers/main/bitnami/pgbouncer/docker-compose.yml > docker-compose.yml
docker-compose up -d

Warning: This quick setup is only intended for development environments. You are encouraged to change the insecure default credentials and check out the available configuration options in the Configuration section for a more secure deployment.

Why use Bitnami Images?

  • Bitnami closely tracks upstream source changes and promptly publishes new versions of this image using our automated systems.
  • With Bitnami images the latest bug fixes and features are available as soon as possible.
  • Bitnami containers, virtual machines and cloud images use the same components and configuration approach - making it easy to switch between formats based on your project needs.
  • All our images are based on minideb -a minimalist Debian based container image that gives you a small base container image and the familiarity of a leading Linux distribution- or scratch -an explicitly empty image-.
  • All Bitnami images available in Docker Hub are signed with Docker Content Trust (DCT). You can use DOCKER_CONTENT_TRUST=1 to verify the integrity of the images.
  • Bitnami container images are released on a regular basis with the latest distribution packages available.

Looking to use PgBouncer in production? Try VMware Tanzu Application Catalog, the enterprise edition of Bitnami Application Catalog.

Why use a non-root container?

Non-root container images add an extra layer of security and are generally recommended for production environments. However, because they run as a non-root user, privileged tasks are typically off-limits. Learn more about non-root containers in our docs.

Learn more about the Bitnami tagging policy and the difference between rolling tags and immutable tags in our documentation page.

You can see the equivalence between the different tags by taking a look at the tags-info.yaml file present in the branch folder, i.e bitnami/ASSET/BRANCH/DISTRO/tags-info.yaml.

Subscribe to project updates by watching the bitnami/containers GitHub repo.

Get this image

The recommended way to get the Bitnami pgbouncer Docker Image is to pull the prebuilt image from the Docker Hub Registry.

docker pull bitnami/pgbouncer:latest

To use a specific version, you can pull a versioned tag. You can view the list of available versions in the Docker Hub Registry.

docker pull bitnami/pgbouncer:[TAG]

If you wish, you can also build the image yourself by cloning the repository, changing to the directory containing the Dockerfile and executing the docker build command. Remember to replace the APP, VERSION and OPERATING-SYSTEM path placeholders in the example command below with the correct values.

git clone https://github.com/bitnami/containers.git
cd bitnami/APP/VERSION/OPERATING-SYSTEM
docker build -t bitnami/APP:latest .

Configuration

Daemon settings

The following parameters can be set for the PgBouncer daemon:

Authentication

The authentication mode can be set using the PGBOUNCER_AUTH_TYPE variable, which can be set to any of the values available in the official PgBouncer documentation. In the case of the scram-sha-256 authentication type (default value), set the backend PostgreSQL credentials as explained in the Backend PostgreSQL connection section.

Backend PostgreSQL connection

The Bitnami PgBouncer container requires a running PostgreSQL installation to connect to. This is configured with the following environment variables.

  • POSTGRESQL_USERNAME: Backend PostgreSQL username. Default: postgres.
  • POSTGRESQL_PASSWORD: Backend PostgreSQL password. No defaults.
  • POSTGRESQL_DATABASE: Backend PostgreSQL Database name to connect to. Default: ${PGBOUNCER_DATABASE}.
  • POSTGRESQL_HOST: Backend PostgreSQL hostname. Default: postgresql.
  • POSTGRESQL_PORT: Backend PostgreSQL port. Default: 5432.
  • PGBOUNCER_SET_DATABASE_USER: Whether to include the backend PostgreSQL username in the database string. Default no.
  • PGBOUNCER_SET_DATABASE_PASSWORD: Whether to include the backend PostgreSQL password in the database string. Default no.
  • PGBOUNCER_CONNECT_QUERY: Query which will be executed after a connection is established. No Defaults.
  • PGBOUNCER_DSN_${i}: PgBouncer configuration string for extra PostgreSQL server, where i is a number starting at zero (0).
  • PGBOUNCER_DSN_${i}_FILE: As an alternative to specifying extra PostgreSQL servers directly using PGBOUNCER_DSN_${i} (see above), specify file paths containing the values, one file per PostgreSQL server. This is in line how other variables get read from $…_FILE if it is provided. By default, when a file is missing, a warning will be printed, and all others will be used. If you set $PGBOUNCER_FAIL_ON_INVALID_DSN_FILE to true, the initialisation process will instead abort with an error.
  • PGBOUNCER_USERLIST_FILE: Custom PgBouncer userlists file with connection credentials for any extra PostgreSQL backend. Required line format (including quotes): "<postresql-user>" "<password>".

Port and address binding

The listening port and listening address can be configured with the following environment variables:

  • PGBOUNCER_PORT: PgBouncer port. Default: 6432.
  • PGBOUNCER_BIND_ADDRESS: PgBouncer bind address. Default: 0.0.0.0.

Extra arguments to PgBouncer startup

In case you want to add extra flags to the PgBouncer command, use the PGBOUNCER_EXTRA_FLAGS variable. Example:

docker run --name pgbouncer \
  -e PGBOUNCER_EXTRA_FLAGS="--verbose" \
  bitnami/pgbouncer:latest

Exposed database

  • PGBOUNCER_DATABASE: PgBouncer exposed database. Default: postgres.

In case you'd like pgbouncer to expose your database with a different name, you can use the PGBOUNCER_DATABASE variable. To expose the same database name as the backend, set PGBOUNCER_DATABASE="$POSTGRESQL_DATABASE". To expose a "fallback database" (wildcard that matches any)), set PGBOUNCER_DATABASE="*".

Other options

  • PGBOUNCER_AUTH_USER: PgBouncer will use this user to connect to the database and query the PostgreSQL backend for a user and password. No defaults.
  • PGBOUNCER_AUTH_DBNAME: PgBouncer will use this user database for authentication purposes. No defaults.
  • PGBOUNCER_AUTH_QUERY: PgBouncer will use this query to connect to the database and query the PostgreSQL backend for a user and password. No defaults.
  • PGBOUNCER_AUTH_TYPE: PgBouncer authentication type. Default: scram-sha-256
  • PGBOUNCER_AUTH_HBA_FILE: HBA configuration file to use if auth_type is set to hba. No defaults.
  • PGBOUNCER_USERLIST: Specify content of the userlist.txt file
  • PGBOUNCER_POOL_MODE : PgBouncer pool mode. Allowed values: session, transaction and statement. Default: session.
  • PGBOUNCER_INIT_SLEEP_TIME : PgBouncer initialization sleep time. Default: 10.
  • PGBOUNCER_INIT_MAX_RETRIES : PgBouncer initialization maximum retries. Default: 10.
  • PGBOUNCER_QUERY_WAIT_TIMEOUT : PgBouncer maximum time queries are allowed to spend waiting for execution. Default: 120.
  • PGBOUNCER_MAX_CLIENT_CONN : PgBouncer maximum number of client connections allowed. Default: 120.
  • PGBOUNCER_MAX_DB_CONNECTIONS : PgBouncer maximum number of database connections allowed. Default: 0 (unlimited).
  • PGBOUNCER_IDLE_TRANSACTION_TIMEOUT : PgBouncer maximum time for a client to be in "idle in transaction" state. Default: 0.0.
  • PGBOUNCER_DEFAULT_POOL_SIZE : PgBouncer maximum server connections to allow per user/database pair. Default: 20.
  • PGBOUNCER_MIN_POOL_SIZE : PgBouncer has at least this amount of open connections. Default: 0 (disabled).
  • PGBOUNCER_RESERVE_POOL_SIZE : PgBouncer allows this amount of additional connections. Default: 0 (disabled).
  • PGBOUNCER_IGNORE_STARTUP_PARAMETERS: you can use this to set ignore_startup_parameters in the auto-generated pgbouncer.ini. This can be useful for solving certain connection issues. See https://www.pgbouncer.org/ config.html for more details.
  • PGBOUNCER_SERVER_IDLE_TIMEOUT: PgBouncer maximum time in seconds a server connection can be idle. If 0 then the timeout is disabled. Default: 600
  • PGBOUNCER_SERVER_RESET_QUERY: PgBouncer query sent to server on connection release before making it available to other clients. Default: DISCARD ALL
  • PGBOUNCER_STATS_USERS: PgBouncer comma-separated list of database users that are allowed to connect and run read-only queries. No defaults.
  • PGBOUNCER_MAX_PREPARED_STATEMENTS: PgBouncer maximum number of cached prepared statements. Default: 0 (disabled).

Initializing a new instance

When the container is launched, it will execute the files with extension .sh located at /docker-entrypoint-initdb.d.

In order to have your custom files inside the docker image you can mount them as a volume.

docker run --name pgbouncer \
  -v /path/to/init-scripts:/docker-entrypoint-initdb.d \
  bitnami/pgbouncer:latest

Or with docker-compose

pgbouncer:
  image: bitnami/pgbouncer:latest
  volumes:
    - /path/to/init-scripts:/docker-entrypoint-initdb.d

Securing PgBouncer traffic

PgBouncer supports the encryption of connections using the SSL/TLS protocol. Should you desire to enable this optional feature, you may use the following environment variables to configure the application:

  • PGBOUNCER_CLIENT_TLS_SSLMODE: TLS traffic settings. Defaults to disable. Check the official PgBouncer documentation for the available values for client_tls_sslmode.

  • PGBOUNCER_CLIENT_TLS_CERT_FILE: File containing the certificate file for the TLS traffic. No defaults.

  • PGBOUNCER_CLIENT_TLS_KEY_FILE: File containing the key for certificate. No defaults.

  • PGBOUNCER_CLIENT_TLS_CA_FILE: File containing the CA of the certificate. If provided, PgBouncer will authenticate TLS/SSL clients by requesting them a certificate . No defaults.

  • PGBOUNCER_CLIENT_TLS_CIPHERS: TLS ciphers to be used. Defaults to fast.Check the official PgBouncer documentation for the available values for client_tls_ciphers.

  • PGBOUNCER_SERVER_TLS_SSLMODE: Server TLS traffic settings. Defaults to disable. Check the official PgBouncer documentation for the available values for server_tls_sslmode.

  • PGBOUNCER_SERVER_TLS_KEY_FILE: File containing the key to authenticate against PostgreSQL server. No defaults.

  • PGBOUNCER_SERVER_TLS_CERT_FILE: File containing the certificate associated to previous private key. PostgreSQL server can validate it. No defaults.

  • PGBOUNCER_SERVER_TLS_CA_FILE: File containing the CA of the server certificate. If provided, PgBouncer will authenticate TLS/SSL clients by requesting them a certificate . No defaults.

  • PGBOUNCER_SERVER_TLS_PROTOCOLS: TLS protocols to be used in server connection. Defaults to secure. Check the official PgBouncer documentation for the available values for server_tls_protocols.

  • PGBOUNCER_SERVER_TLS_CIPHERS: TLS ciphers to be used in server connection. Defaults to fast. Check the official PgBouncer documentation for the available values for server_tls_ciphers.

When enabling TLS, PgBouncer will support both standard and encrypted traffic by default but prefer the latter. Below there are some examples of how to quickly set up client TLS traffic:

  1. Using docker run

    $ docker run \
        -v /path/to/certs:/opt/bitnami/pgbouncer/certs \
        -e PGBOUNCER_CLIENT_TLS_SSLMODE=require \
        -e PGBOUNCER_CLIENT_TLS_CERT_FILE=/opt/bitnami/pgbouncer/certs/pgbouncer.crt \
        -e PGBOUNCER_CLIENT_TLS_KEY_FILE=/opt/bitnami/pgbouncer/certs/pgbouncer.key \
        bitnami/pgbouncer:latest
    
  2. Modifying the docker-compose.yml file present in this repository:

    services:
      pgbouncer:
      ...
        environment:
          ...
          - PGBOUNCER_CLIENT_TLS_SSLMODE=require
          - PGBOUNCER_CLIENT_TLS_CERT_FILE=/opt/bitnami/pgbouncer/certs/pgbouncer.crt
          - PGBOUNCER_CLIENT_TLS_KEY_FILE=/opt/bitnami/pgbouncer/certs/pgbouncer.key
        ...
        volumes:
          ...
          - /path/to/certs:/opt/bitnami/pgbouncer/certs
      ...
    

Alternatively, you may also provide this configuration in your custom configuration file.

Configuration file

The image looks for pgbouncer.ini file in /opt/bitnami/pgbouncer/conf/. You can mount a volume at /bitnami/pgbouncer/conf/ and copy/edit the pgbouncer.ini file in the /path/to/pgbouncer-persistence/conf/. The default configurations will be populated to the conf/ directory if it's empty.

/path/to/pgbouncer-persistence/conf/
└── pgbouncer.ini

0 directories, 1 file

As PgBouncer image is non-root, you need to set the proper permissions to the mounted directory in your host:

sudo chown 1001:1001 /path/to/pgbouncer-persistence/conf/

Step 1: Run the PgBouncer image

Run the PgBouncer image, mounting a directory from your host.

docker run --name pgbouncer \
    -v /path/to/pgbouncer-persistence/conf/:/bitnami/pgbouncer/conf/ \
    bitnami/pgbouncer:latest

or using Docker Compose:

version: '2'

...

services:
  pgbouncer:
    image: 'bitnami/pgbouncer:latest'
    ports:
      - '6432:6432'
    volumes:
      - /path/to/pgbouncer-persistence/conf/:/bitnami/pgbouncer/conf/

Step 2: Edit the configuration

Edit the configuration on your host using your favorite editor.

vi /path/to/pgbouncer-persistence/conf/pgbouncer.ini

Step 3: Restart PgBouncer

After changing the configuration, restart your PgBouncer container for changes to take effect.

docker restart pgbouncer

or using Docker Compose:

docker-compose restart pgbouncer

Refer to the server configuration manual for the complete list of configuration options.

How to connect with multiple PostgreSQL servers

It is possible to connect a single PgBouncer instance with multiple PostgreSQL backends. By using as many PGBOUNCER_DSN_${i} environment variables (with i starting at zero, 0) as needed, and the PGBOUNCER_USERLIST_FILE variable pointing to a mounted volume with the required credentials for any extra PostgreSQL database in the format "<postgresql-user>" "<password>".

The PgBouncer initialization process requires one PostgreSQL backend to be configured using the different POSTGRESQL_* variables listed in the backend PostgreSQL connection, but the rest of backends connections can be provided using the method explained in this section. An example docker-compose.yaml for this scenario can be found below

  pg1:
    image: docker.io/bitnami/postgresql:14
    volumes:
      - 'pg1_data:/bitnami/postgresql'
    environment:
      - POSTGRESQL_PASSWORD=password1
      - POSTGRESQL_DATABASE=db1

  pg2:
    image: docker.io/bitnami/postgresql:15
    volumes:
      - 'pg2_data:/bitnami/postgresql'
    environment:
      - POSTGRESQL_PASSWORD=password2
      - POSTGRESQL_DATABASE=db2

  pg3:
    image: docker.io/bitnami/postgresql:14
    volumes:
      - 'pg3_data:/bitnami/postgresql'
    environment:
      - POSTGRESQL_PASSWORD=password3
      - POSTGRESQL_DATABASE=db3

  pgbouncer:
    image: docker.io/bitnami/pgbouncer:1
    ports:
      - 6432:6432
    volumes:
      - './userlists.txt:/bitnami/userlists.txt'
    environment:
      - POSTGRESQL_HOST=pg1
      - POSTGRESQL_PASSWORD=password1
      - POSTGRESQL_DATABASE=db1
      - PGBOUNCER_AUTH_TYPE=trust
      - PGBOUNCER_USERLIST_FILE=/bitnami/userlists.txt
      - PGBOUNCER_DSN_0=pg1=host=pg1 port=5432 dbname=db1
      - PGBOUNCER_DSN_1=pg2=host=pg2 port=5432 dbname=db2
      - PGBOUNCER_DSN_2=pg3=host=pg3 port=5432 dbname=db3
volumes:
  pg1_data:
    driver: local
  pg2_data:
    driver: local
  pg3_data:
    driver: local

And this is the content of the userlists.txt file:

"postgres" "password1"
"postgres" "password2"
"postgres" "password3"

Once initialized, the scenario above provides access to three diferent PostgreSQL backends from a single PgBouncer instance. As an example, you can request the PostgreSQL version of the backend server number two (notice it is the only running PostgreSQL 15.x in this scenario):

$ docker exec -it -u root debian-11-pgbouncer-1 psql -p 6432 -U postgres pg2 -c "show server_version;"
 server_version
 ----------------
  15.4
  (1 row)

Contributing

We'd love for you to contribute to this container. You can request new features by creating an issue or submitting a pull request with your contribution.

Issues

If you encountered a problem running this container, you can file an issue. For us to provide better support, be sure to fill the issue template.

License

Copyright © 2023 VMware, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.