From d275b0b42c56726c696f589febdad198e39751b4 Mon Sep 17 00:00:00 2001 From: rene-rene-rene <120447665+rene-rene-rene@users.noreply.github.com> Date: Mon, 25 May 2026 18:19:16 +0300 Subject: [PATCH 1/3] Update entrypoint.sh add pool_mode in DATABASE_URL --- entrypoint.sh | 70 ++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 7e6ae1a..914f673 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -20,34 +20,34 @@ fi # # Parameters: # - The url we should parse -# Returns (sets variables): DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME -function parse_url() { - # Thanks to https://stackoverflow.com/a/17287984/146289 - - # Allow to pass values like dj-database-url / django-environ accept - proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" - url="$(echo $1 | sed -e s,$proto,,g)" - - # extract the user and password (if any) - userpass="$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')" - DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)" - if [ -n "${DB_PASSWORD}" ]; then - DB_USER="$(echo $userpass | grep : | cut -d: -f1)" - else - DB_USER="${userpass}" - fi - - # extract the host -- updated - hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` - port=`echo $hostport | grep : | cut -d: -f2` - if [ -n "$port" ]; then - DB_HOST=`echo $hostport | grep : | cut -d: -f1` - DB_PORT="${port}" - else - DB_HOST="${hostport}" - fi - - DB_NAME="$(echo $url | grep / | cut -d/ -f2-)" +# Returns (sets variables): DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME, POOL_MODE +function parse_url() { + proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" + url="$(echo $1 | sed -e s,$proto,,g)" + userpass="$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')" + DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)" + if [ -n "${DB_PASSWORD}" ]; then + DB_USER="$(echo $userpass | grep : | cut -d: -f1)" + else + DB_USER="${userpass}" + fi + hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` + port=`echo $hostport | grep : | cut -d: -f2` + if [ -n "$port" ]; then + DB_HOST=`echo $hostport | grep : | cut -d: -f1` + DB_PORT="${port}" + else + DB_HOST="${hostport}" + DB_PORT=5432 + fi + # Parse database name and optional pool_mode + path=$(echo $url | cut -d/ -f2-) + DB_NAME=$(echo "$path" | cut -d/ -f1) + POOL_MODE=$(echo "$path" | cut -d/ -f2) + # If pool_mode is same as db_name or empty, clear it + if [ -z "$POOL_MODE" ] || [ "$POOL_MODE" = "$DB_NAME" ]; then + POOL_MODE="" + fi } # Grabs variables set by `parse_url` and adds them to the userlist if not already set in there. @@ -64,12 +64,14 @@ function generate_userlist_if_needed() { } # Grabs variables set by `parse_url` and adds them to the PG config file as a database entry. -function generate_config_db_entry() { - printf "\ -${DB_NAME:-*} = host=${DB_HOST:?"Setup pgbouncer config error! You must set DB_HOST env"} \ -port=${DB_PORT:-5432} auth_user=${DB_USER:-postgres} -${CLIENT_ENCODING:+client_encoding = ${CLIENT_ENCODING}\n}\ -" >> "${PG_CONFIG_FILE}" +function generate_config_db_entry() { + printf "%s = host=%s port=%s auth_user=%s%s%s\n" \ + "${DB_NAME:-*}" \ + "${DB_HOST:?"Setup pgbouncer config error! You must set DB_HOST env"}" \ + "${DB_PORT:-5432}" \ + "${DB_USER:-postgres}" \ + "${POOL_MODE:+ pool_mode=${POOL_MODE}}" \ + "${CLIENT_ENCODING:+ client_encoding=${CLIENT_ENCODING}}" >> "${PG_CONFIG_FILE}" } # Write the password with MD5 encryption, to avoid printing it during startup. From e2ff2fc8b19440a66a07a8cd63cd91f46e0cf2e8 Mon Sep 17 00:00:00 2001 From: Roman Shamrai Date: Mon, 25 May 2026 18:34:17 +0300 Subject: [PATCH 2/3] remove spaces, add comment to README.md --- README.md | 3 +++ entrypoint.sh | 54 +++++++++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a6362fd..b7dc733 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,9 @@ docker run --rm \ edoburu/pgbouncer ``` +As an option, you can add pool_mode at the end using a slash.\ +DATABASE_URL="postgres://user:pass@postgres-host/database/**session**" + Kubernetes integration ---------------------- diff --git a/entrypoint.sh b/entrypoint.sh index 914f673..651c491 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -21,33 +21,33 @@ fi # Parameters: # - The url we should parse # Returns (sets variables): DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME, POOL_MODE -function parse_url() { - proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" - url="$(echo $1 | sed -e s,$proto,,g)" - userpass="$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')" - DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)" - if [ -n "${DB_PASSWORD}" ]; then - DB_USER="$(echo $userpass | grep : | cut -d: -f1)" - else - DB_USER="${userpass}" - fi - hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` - port=`echo $hostport | grep : | cut -d: -f2` - if [ -n "$port" ]; then - DB_HOST=`echo $hostport | grep : | cut -d: -f1` - DB_PORT="${port}" - else - DB_HOST="${hostport}" - DB_PORT=5432 - fi - # Parse database name and optional pool_mode - path=$(echo $url | cut -d/ -f2-) - DB_NAME=$(echo "$path" | cut -d/ -f1) - POOL_MODE=$(echo "$path" | cut -d/ -f2) - # If pool_mode is same as db_name or empty, clear it - if [ -z "$POOL_MODE" ] || [ "$POOL_MODE" = "$DB_NAME" ]; then - POOL_MODE="" - fi +function parse_url() { + proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" + url="$(echo $1 | sed -e s,$proto,,g)" + userpass="$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')" + DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)" + if [ -n "${DB_PASSWORD}" ]; then + DB_USER="$(echo $userpass | grep : | cut -d: -f1)" + else + DB_USER="${userpass}" + fi + hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` + port=`echo $hostport | grep : | cut -d: -f2` + if [ -n "$port" ]; then + DB_HOST=`echo $hostport | grep : | cut -d: -f1` + DB_PORT="${port}" + else + DB_HOST="${hostport}" + DB_PORT=5432 + fi + # Parse database name and optional pool_mode + path=$(echo $url | cut -d/ -f2-) + DB_NAME=$(echo "$path" | cut -d/ -f1) + POOL_MODE=$(echo "$path" | cut -d/ -f2) + # If pool_mode is same as db_name or empty, clear it + if [ -z "$POOL_MODE" ] || [ "$POOL_MODE" = "$DB_NAME" ]; then + POOL_MODE="" + fi } # Grabs variables set by `parse_url` and adds them to the userlist if not already set in there. From 7969c9bbee10532d44cc46fd4712d5fe6546995e Mon Sep 17 00:00:00 2001 From: Roman Shamrai Date: Tue, 26 May 2026 09:38:38 +0300 Subject: [PATCH 3/3] [FIX] broken function generate_config_db_entry --- entrypoint.sh | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 651c491..48c795c 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -21,9 +21,11 @@ fi # Parameters: # - The url we should parse # Returns (sets variables): DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME, POOL_MODE + function parse_url() { proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" url="$(echo $1 | sed -e s,$proto,,g)" + userpass="$(echo $url | grep @ | sed -r 's/^(.*)@([^@]*)$/\1/')" DB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)" if [ -n "${DB_PASSWORD}" ]; then @@ -31,6 +33,7 @@ function parse_url() { else DB_USER="${userpass}" fi + hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` port=`echo $hostport | grep : | cut -d: -f2` if [ -n "$port" ]; then @@ -40,10 +43,12 @@ function parse_url() { DB_HOST="${hostport}" DB_PORT=5432 fi + # Parse database name and optional pool_mode path=$(echo $url | cut -d/ -f2-) DB_NAME=$(echo "$path" | cut -d/ -f1) POOL_MODE=$(echo "$path" | cut -d/ -f2) + # If pool_mode is same as db_name or empty, clear it if [ -z "$POOL_MODE" ] || [ "$POOL_MODE" = "$DB_NAME" ]; then POOL_MODE="" @@ -64,14 +69,14 @@ function generate_userlist_if_needed() { } # Grabs variables set by `parse_url` and adds them to the PG config file as a database entry. -function generate_config_db_entry() { - printf "%s = host=%s port=%s auth_user=%s%s%s\n" \ - "${DB_NAME:-*}" \ - "${DB_HOST:?"Setup pgbouncer config error! You must set DB_HOST env"}" \ - "${DB_PORT:-5432}" \ - "${DB_USER:-postgres}" \ - "${POOL_MODE:+ pool_mode=${POOL_MODE}}" \ - "${CLIENT_ENCODING:+ client_encoding=${CLIENT_ENCODING}}" >> "${PG_CONFIG_FILE}" +function generate_config_db_entry() { + printf "%s = host=%s port=%s auth_user=%s%s%s\n" \ + "${DB_NAME:-*}" \ + "${DB_HOST:?"Setup pgbouncer config error! You must set DB_HOST env"}" \ + "${DB_PORT:-5432}" \ + "${DB_USER:-postgres}" \ + "${POOL_MODE:+ pool_mode=${POOL_MODE}}" \ + "${CLIENT_ENCODING:+ client_encoding=${CLIENT_ENCODING}}" >> "${PG_CONFIG_FILE}" } # Write the password with MD5 encryption, to avoid printing it during startup. @@ -202,4 +207,4 @@ ${TCP_USER_TIMEOUT:+tcp_user_timeout = ${TCP_USER_TIMEOUT}\n}\ fi echo "Starting $*..." -exec "$@" +exec "$@" \ No newline at end of file