Skip to content

Instantly share code, notes, and snippets.

@kamranzafar
Created August 13, 2025 08:20
Show Gist options
  • Save kamranzafar/f314b51b692f14e81ee0a3f54c1a2320 to your computer and use it in GitHub Desktop.
Save kamranzafar/f314b51b692f14e81ee0a3f54c1a2320 to your computer and use it in GitHub Desktop.
Docker Postgres SSL Setup

Postgres SSL Setup (Local)

Create a cert

openssl req -new -509 -days 365 -nodes -text -out server.crt (n -keyout server key -subj "/CN=localhost"

Setup Postgres

Run the following commands to copy cert files to the container and update permissions:

docker cp server.crt «CONTAINER_ID>:/var/lib/pgsql/data/userdata/ 
docker cp server. key «CONTAINER_ID>:/var/lib/pgsql/data/userdata/
docker exec -u 0 -it «CONTAINER_ ID> chown postgres:postgres /var/lib/pgsql/data/userdata/server.crt 
docker exec -u 0 -it «CONTAINER ID> chown postgres:postgres /var/lib/pgsql/data/userdata/server.key 
docker exec -u 0 -it «CONTAINER ID» chmod og-rwx /var/11b/pgsql/data/userdata/server.key

Login to the postgres docker container

docker exec -it «CONTAINER_ ID> bash

Update the following SSL config in /var/lib/postgresql/data/postgresql.conf

ssl = on
ssl_cert_file = server.crt
ssl_key_file = server.key

Restart the postgres container.

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