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;
)
In the User variables section, select the environment variable you want to modify. Click Edit to open the Edit User Variable dialog box. Change the value of the variable and click OK. The variable is updated in the User variables section of the Environment Variables dialog box.
There are two ways we can define and assign values to variables. We can define variables implicitly by using the –v switch of sqlcmd. Also we can define and assign a value to variables explicitly using the :setvar sqlcmd command.
Set the environment variables in the installation owner user profile file on each cluster node. You need to set ORACLE_BASE and ORACLE_HOME , and ORACLE_SID ; also add ORACLE_HOME/bin to the path environment variable.
Variables that are used in scripts are called scripting variables. Scripting variables enable one script to be used in multiple scenarios. For example, if you want to run one script against multiple servers, instead of modifying the script for each server, you can use a scripting variable for the server name.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With