Skip to content

Instantly share code, notes, and snippets.

@lfittl
Last active December 31, 2023 15:33
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lfittl/1b0671ac07b33521ea35fcd22b0120f5 to your computer and use it in GitHub Desktop.
Save lfittl/1b0671ac07b33521ea35fcd22b0120f5 to your computer and use it in GitHub Desktop.
Enabling pg_stat_statements in a Docker container
version: '2'
services:
db:
image: postgres:16
ports:
- "5432:5432"
command: >
postgres
-c shared_preload_libraries='pg_stat_statements'
volumes:
- /var/lib/postgresql/data
environment:
POSTGRES_USER: myproject
@Laro88
Copy link

Laro88 commented Jan 9, 2020

If you are on alpine and dont have perl then use:
sed -i "s/#shared_preload_libraries = ''/shared_preload_libraries = 'pg_stat_statements'/g" /var/lib/postgresql/data/postgresql.conf

@lukaszmoginas
Copy link

I'm still getting ERROR: pg_stat_statements must be loaded via shared_preload_libraries. Tried some different solution, but still couldn't find one, which solves this error

@lukaszmoginas
Copy link

lukaszmoginas commented Jan 18, 2020

Found another way (it worked for me):

version: '2'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: myproject

@hackwaly
Copy link

docker cp postgres:/var/lib/postgresql/data/postgresql.conf postgresql.conf
vi postgresql.conf
docker cp postgresql.conf postgres:/var/lib/postgresql/data/postgresql.conf
docker restart postgres

@lcalv82
Copy link

lcalv82 commented Jan 7, 2021

This helped. Great article.

Finally, login to the container with postgres user and enable the extension with command:
CREATE EXTENSION pg_stat_statements;
Check if good:
SELECT *
FROM pg_available_extensions
WHERE
name = 'pg_stat_statements' and
installed_version is not null;

@gerrard00
Copy link

Found another way (it worked for me):

version: '2'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: myproject

This worked for me.

@ivenspontes
Copy link

Found another way (it worked for me):

version: '2'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: myproject

thank you, worked fine here

@DejavuMoe
Copy link

Found another way (it worked for me):

version: '2'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: myproject

Works for me, too! tks⛄︎

@misostack
Copy link

Notes

@stradox4u
Copy link

Found another way (it worked for me):

version: '2'

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: myproject

Works for me, too! tks⛄︎

Another +1 for this solution.

Copy link

ghost commented Dec 19, 2023

Same here, postgres -c shared_preload_libraries=pg_stat_statements portion allowed me to use the view.

@lfittl
Copy link
Author

lfittl commented Dec 20, 2023

Thanks all for the comments - updated the gist to make it easier for anyone landing here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment