Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Created October 9, 2024 08:36
Show Gist options
  • Save icyleaf/7a010fa6d0bd938eb114323d094c8589 to your computer and use it in GitHub Desktop.
Save icyleaf/7a010fa6d0bd938eb114323d094c8589 to your computer and use it in GitHub Desktop.
Intialize postgresql database and grant privileges in two ways by docker container

Intialize postgresql database and grant privileges

first of all, you need super account, for example user postgres and password insecret-pass.

Easy mode

Use super account to connect postgres server and create database app-name and user app, then grant app to app-name.

docker run --rm \
  -e INIT_POSTGRES_HOST=postgres.example.com \
  -e INIT_POSTGRES_PORT=5432 \
  -e INIT_POSTGRES_DBNAME=app-name \
  -e INIT_POSTGRES_USER=app \
  -e INIT_POSTGRES_PASS=pass \
  -e INIT_POSTGRES_SUPER_USER=postgres \
  -e INIT_POSTGRES_SUPER_PASS=insecret-pass \
  ghcr.io/onedr0p/postgres-init

Custom mode

echo "CREATE USER app WITH CREATEDB SUPERUSER PASSWORD pass;
CREATE DATABASE app-name OWNER app;" > init.sql

docker run --rm \
  -e SENZING_SQL_FILES=/init.sql \
  -e SENZING_DATABASE_URL=postgresql://postgres:insecret-pass@postgres.example.com \
  -v init.sql:/init.sql \
  senzing/postgresql-client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment