I have a docker container, and -e DEV_ENV is defined with the docker run command, and this the unit file I built to run a script...
[Unit]
Description=Script that creates environement users and databases.
After=postgresql.service
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/su - postgres -c 'export DEV_ENV=$0; /usr/local/bin/psql.sh' "$DEV_ENV"
TimeoutStartSec=100
[Install]
WantedBy=default.target
I'm trying to run the following script...
#!/bin/bash
while [[ $(systemctl status postgresql.service | awk '/Active/sub("\\(","") { print $3 }' | awk 'sub("\\)","")' ) != "running" ]]
do echo "Waiting on service to come up.";done
for i in ${DEV_ENV[*]};do EX=$(psql -lt | cut -d\| -f1 | grep ${i,,})
echo "Creating ${i}"
if [[ ! ${EX} ]];then
psql -c "CREATE DATABASE ${i};"
psql -c "CREATE USER $i WITH PASSWORD '${i}'; GRANT ALL PRIVILEGES ON DATABASE $i TO ${i};"
fi;done
I know the script runs, I put in checks at beginning and end of the script.
Unfortunately the Unit file fails because the $DEV_ENV variable is not being passed.
I can run the following manually and it works.
/usr/bin/su - postgres -c 'export DEV_ENV=$0; /usr/local/bin/psql.sh' "$DEV_ENV"
How do I properly pass the environment variable to the postgres shell?
DEV_ENVvariable has a meaningful value?