Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert bash-variables in psql-command

I'm going crazy while trying to insert bash-variables in a psql commands as connection paramters as well as variables in the command itself. The following example works properly:

psql -U postgres -h localhost -p 5432 -c "CREATE DATABASE  testdb WITH ENCODING='UTF8' OWNER=postgres TABLESPACE=pg_default TEMPLATE=template_postgis CONNECTION LIMIT=-1;"

Now I'm trying to exchange each parameter through a variable, which is held in special config-file.

Non-working example:

dbserver=localhost
dbport=5432
dbowner=postgres
dbname=testdb
dbtemplate=template_postgis
dbtablespace=pg_default

 psql -U '$dbowner' -h '$dbserver' -p '$dbport' -c "CREATE DATABASE  '$dbname' WITH ENCODING='UTF8' OWNER='§dbowner' TABLESPACE='$dbtablespace' TEMPLATE='$dbtemplate'
CONNECTION LIMIT=-1;"

I've tried several quotings, backquotes and escape-slashes already but smhow it still won't work. Thanks in advance, knutella

like image 777
knutella Avatar asked May 01 '26 17:05

knutella


1 Answers

Use double quotes ("). Single quotes (') does not interpret shell variables inside.

Try it

 echo '$USER' "$USER"

See man bash.

like image 66
Grzegorz Żur Avatar answered May 03 '26 18:05

Grzegorz Żur