I've created a database initialization script that creates the database and adds an entry to a specific table.
insert into user (email) values (${USER_EMAIL})
My docker-compose file is similar to this one:
version: '3.7'
services:
postgres:
image: postgres:10.5
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5438:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
# copy the sql script to create tables
- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
# copy the sql script to fill tables
- ./sql/fill_tables.sql:/docker-entrypoint-initdb.d/fill_tables.sql
USER_EMAIL is an env variable is is not being set in the script. What's the right way to do this?
CodePudding user response:
if you want to give a value for USER_EMAIL, you can simply add it in environment section.
version: '3.7'
services:
postgres:
image: postgres:10.5
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- USER_EMAIL
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5438:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
# copy the sql script to create tables
- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
# copy the sql script to fill tables
- ./sql/fill_tables.sql:/docker-entrypoint-initdb.d/fill_tables.sql
in your script just set a value for USER_EMAIL.
CodePudding user response:
The USER_EMAIL
is not going to be available in sql file. I think you already tried that. Try using envsubst
. So something like
command: >
/bin/bash -c "envsubst < '$USER\_EMAIL' > ./initdb/fill_tables.sql"
I haven't tried pulling that pg image. So not sure if envsubst
is available inside.