3

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

1
  • And the error you get is... Commented May 14, 2013 at 11:44

2 Answers 2

3

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

Try it

 echo '$USER' "$USER"

See man bash.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this also this works fine for me now. As I wrote in above's comments: ...my variables were sourced in incorrectly and empty..
3

This works... most of the quotes are not needed:

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

1 Comment

Thanks this does the trick now. actually i tried it in the very first already, but problem was, that paramters are smhow where not proberly sourced in from config-file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.