How do I pass an environment variable into a .sql file?

Viewed 6919

I'm setting environment variables in a Dockerfile which I want to reference in a .sql file. That file is in /docker-entrypoint-initdb.d since initdb will run it.

How can pass an environment variable? (e.g. SELECT * FROM $myEnvironmentVariableHere;)

1 Answers

There are a few ways of doing this, but the easiest may be to use a simple shell script with a HEREDOC:

Here's the script, called in scripts/test.sh

#!/bin/bash

psql << EOF
SELECT * FROM $MYVAR LIMIT 1;
EOF

Here's how to run it:

docker run --rm  --name test_pg -v $PWD/scripts:/docker-entrypoint-initdb.d -e "MYVAR=pg_class" postgres:latest

Among the various startup messages, you'll see the following lines:

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/test.sh
   relname    | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhasrules | relhastriggers | relhassubclass | relrowsecurity | relforcerowsecurity | relispopulated | relreplident | relispartition | relrewrite | relfrozenxid | relminmxid |           relacl            | reloptions | relpartbound
--------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+----------------+---------+----------+-----------+------------+-------------+----------------+----------------+----------------+---------------------+----------------+--------------+----------------+------------+--------------+------------+-----------------------------+------------+--------------
 pg_statistic |           11 |   11319 |         0 |       10 |     0 |        2619 |             0 |       16 |       398 |            16 |          2840 | t           | f           | p              | r       |       26 |         0 | f          | f           | f              | f              | f              | f                   | t              | n            | f              |          0 |          562 |          1 | {postgres=arwdDxt/postgres} |            |
(1 row)
Related