Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------

Expand Down
33 changes: 20 additions & 13 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ 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
# Returns (sets variables): DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME, POOL_MODE

# Allow to pass values like dj-database-url / django-environ accept
function parse_url() {
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
Expand All @@ -37,17 +34,25 @@ function parse_url() {
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}"
DB_PORT=5432
fi

DB_NAME="$(echo $url | grep / | cut -d/ -f2-)"
# 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.
Expand All @@ -65,11 +70,13 @@ 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}"
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.
Expand Down Expand Up @@ -200,4 +207,4 @@ ${TCP_USER_TIMEOUT:+tcp_user_timeout = ${TCP_USER_TIMEOUT}\n}\
fi

echo "Starting $*..."
exec "$@"
exec "$@"