简体   繁体   中英

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

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? (eg SELECT * FROM $myEnvironmentVariableHere; )

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM