Skip to content

About

PostgreSQL docker image with openremote env variables and post GIS

Resources

Stars

3 stars

Watchers

7 watching

Forks

Latest commit

 

History

99 Commits

Folders and files

Repository files navigation

Postgresql docker image

build multiarch postgresql Docker image and push to dockerhub

POSTGIS and TimescaleDB (inc. toolkit for hyperfunctions) image built for amd64 and arm64 using timescale/timescaledb-ha base image with:

  • OR specific ENV variables and a healthcheck added
  • Easy configuration of max_connections using POSTGRES_MAX_CONNECTIONS environment variable (set to -1 to disable this setting)
  • Sessions left idle in a transaction for more than 10 minutes are terminated, as they prevent autovacuum removing dead rows and cause table bloat; configure using POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT environment variable with a PostgreSQL time value e.g. 30min (set to 0 to disable this setting)
  • Memory settings with defaults sized for a 4 GB host giving PostgreSQL a ~768 MB container memory limit (see below)
  • Diagnostics logging of slow statements (with their plans), lock waits and temp file spills (see below)
  • PGDATA path set to match old Alpine image (for ease of DB migration)
  • POSTGRES user UID and GID changed to match old Alpine image (for ease of DB migration)
  • Auto upgrade of database with PG major version changes from previous PG major version; can be disabled using OR_DISABLE_AUTO_UPGRADE=true
  • Auto upgrade of timescaleDB extension when a new version is available in the container; can be disabled using OR_DISABLE_AUTO_UPGRADE=true
  • Slimmed images using slim toolkit to reduce image size by ~60%

Server settings

These environment variables are passed to PostgreSQL as -c arguments. They come before any arguments in the container command, so a -c given there takes precedence. Set a variable to an empty value or -1 to leave the setting to postgresql.conf / ALTER SYSTEM instead.

The memory defaults suit a 4 GB host where PostgreSQL has a ~768 MB container memory limit. For other limits, scale them: shared_buffers ~25% of the limit, effective_cache_size ~75% and maintenance_work_mem ~1/12.

Environment variable PostgreSQL setting Default
POSTGRES_MAX_CONNECTIONS max_connections 50
POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT idle_in_transaction_session_timeout 10min
POSTGRES_SHARED_BUFFERS shared_buffers 192MB
POSTGRES_EFFECTIVE_CACHE_SIZE effective_cache_size 576MB
POSTGRES_MAINTENANCE_WORK_MEM maintenance_work_mem 64MB
POSTGRES_WORK_MEM work_mem 4MB
POSTGRES_LOG_LINE_PREFIX log_line_prefix (backend PID and client host; Docker adds the timestamp) [%p] [%h]
POSTGRES_LOG_MIN_DURATION_STATEMENT log_min_duration_statement, and auto_explain.log_min_duration with auto_explain loaded through session_preload_libraries 5s
POSTGRES_LOG_LOCK_WAITS log_lock_waits on
POSTGRES_LOG_TEMP_FILES log_temp_files 10MB
POSTGRES_LOG_CHECKPOINTS log_checkpoints off

shared_buffers does not live in /dev/shm, so the container's shm_size does not need to grow with it; /dev/shm is only used for dynamic shared memory such as parallel query.

Local Development

Prerequisites

  • Docker must be installed and running
  • slim toolkit must be installed for image optimization

Install slim toolkit via the install script:

curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo -E bash -

Or via Homebrew (macOS):

brew install docker-slim

For more installation options, see the slim toolkit documentation.

Building the Image

  1. Build the Docker image (replace 17 with desired PostgreSQL major version):
docker build --build-arg PG_MAJOR=17 -t openremote/postgresql:pg17 .
  1. Slim the image using the provided script:
# Usage: ./slim-image.sh <source-image> <target-image> <architecture>
# Architecture: amd64 or arm64 (auto-detected if omitted)

./slim-image.sh openremote/postgresql:pg17 openremote/postgresql:pg17-slim
  1. Optionally replace the original with the slimmed version:
docker tag openremote/postgresql:pg17-slim openremote/postgresql:pg17

Upgrading

NOTE: If you change the version of container you use then make sure you have backed up your DB first as this container will try to auto upgrade your DB and/or TimescaleDB extension; this auto upgrade functionality can be disabled using OR_DISABLE_AUTO_UPGRADE=true

Automatic Upgrade

This image supports automatic upgrades from the previous PostgreSQL major version. When the container starts with an existing database from a supported older version, it will:

  1. Upgrade TimescaleDB extensions on the old PostgreSQL version
  2. Run pg_upgrade to migrate the database to the new PostgreSQL version
  3. Upgrade TimescaleDB extensions on the new PostgreSQL version

Manual Upgrade

If automatic upgrade is not supported for your database version (e.g., skipping multiple major versions), you will need to perform a manual upgrade. Follow these steps:

  1. Backup your database using pg_dump or pg_dumpall
  2. Upgrade TimescaleDB first (if installed) - this must be done before PostgreSQL upgrade
  3. Use pg_upgrade to migrate between PostgreSQL versions, or restore from backup to a fresh database

Useful Resources

Example: Manual pg_dump/restore

# On the old container, dump the database
docker exec -it <old_container> pg_dumpall -U postgres > backup.sql

# Start the new container with a fresh data directory
docker run -d --name new_postgres -v /path/to/new/data:/var/lib/postgresql/data openremote/postgresql:latest

# Restore the backup
docker exec -i new_postgres psql -U postgres < backup.sql

About

PostgreSQL docker image with openremote env variables and post GIS

Resources

Stars

3 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages