Created
August 27, 2024 16:00
-
-
Save Maxiviper117/a240826be644a98b69e97475683cb062 to your computer and use it in GitHub Desktop.
Docker Compose Setup for PostgreSQL with PgBouncer and Adminer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Docker Compose file for setting up PostgreSQL with PgBouncer and Adminer | |
| version: '3.8' # Specify the Docker Compose version | |
| services: | |
| # PostgreSQL database service | |
| postgredb: | |
| image: postgres:latest # Use the latest PostgreSQL image | |
| environment: | |
| # Environment variables are loaded from the .env file | |
| POSTGRES_USER: ${POSTGRES_USER} # Database user (from .env) | |
| POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} # Database password (from .env) | |
| POSTGRES_DB: ${POSTGRES_DB} # Database name (from .env) | |
| POSTGRES_HOST_AUTH_METHOD: md5 # Authentication method | |
| POSTGRES_INITDB_ARGS: --auth=md5 # Initialize DB with MD5 auth | |
| volumes: | |
| - ./postgres-data:/var/lib/postgresql/data # Persist data in a local folder | |
| # PgBouncer service for connection pooling | |
| pgbouncer: | |
| image: edoburu/pgbouncer:latest # Use the latest PgBouncer image | |
| ports: | |
| - "5434:5432" # Expose PgBouncer on port 5434 | |
| environment: | |
| # Use environment variables from the .env file | |
| DB_USER: ${POSTGRES_USER} # Reuse the PostgreSQL user | |
| DB_PASSWORD: ${POSTGRES_PASSWORD} # Reuse the PostgreSQL password | |
| DB_HOST: postgredb # Link to the PostgreSQL service | |
| DB_NAME: ${POSTGRES_DB} # Reuse the PostgreSQL database name | |
| ADMIN_USERS: postgres,admin # Admin users for PgBouncer | |
| # Adminer service for database management | |
| adminer: | |
| image: adminer:latest # Use the latest Adminer image | |
| restart: always # Always restart Adminer if it stops | |
| ports: | |
| - 8080:8080 # Expose Adminer on port 8080 | |
| environment: | |
| ADMINER_DEFAULT_SERVER: pgbouncer # Set PgBouncer as the default server | |
| ADMINER_DEFAULT_SYSTEM: pgsql # Set PostgreSQL as the default system | |
| depends_on: | |
| - pgbouncer # Ensure PgBouncer starts before Adminer | |
| # .env file structure: | |
| # -------------------- | |
| # POSTGRES_USER=myuser | |
| # POSTGRES_PASSWORD=mypassword | |
| # POSTGRES_DB=mydatabase |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important Note on Authentication
Starting with PostgreSQL version 14, the default authentication method has changed to
scram-sha-256. However, PgBouncer expectsmd5authentication by default. To avoid authentication errors such asNpgsql.PostgresException: '08P01: server login failed: wrong password type', make sure to configure PostgreSQL to usemd5authentication. This can be done by setting thePOSTGRES_HOST_AUTH_METHODandPOSTGRES_INITDB_ARGSenvironment variables in your Docker Compose file.If you prefer to use
scram-sha-256with PgBouncer, you will need to use a compatible PgBouncer image, such asrmccaffrey/pgbouncer:latest, and adjust your configuration accordingly.See: https://stackoverflow.com/questions/76046768/configure-pgbouncer-and-postgresql-in-docker-compose