Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 656
Sam2817 Avatar asked Jun 26 '19 11:06

Sam2817


People also ask

How do you pass an environment variable?

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.

How do I declare a variable in Sqlcmd?

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.

Which file required to set the environment variable is?

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.

What is Sqlcmd 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.


Video Answer


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)
like image 95
Jeremy Avatar answered Oct 23 '22 02:10

Jeremy