1

I am trying to execute a .sql file in Heroku PSQL and want to pass dynamic parameter values in the .sql file.

Below is the script which I am using

heroku pg:psql --app application_name <./somepath/file_to_execute.sql --param1="'$File_name'" --param2="'$Tag_id'" --param3="'$job_name'" --param4="$id"

The sql file contains insert script:

INSERT INTO version_table (col1, col2, col3, col4) 
VALUES (:param1,:param2,:param3,:param4);

I get below error message from Heroku:

Error: Unexpected arguments: --param2='1.1.1', --param3='test-name', --param4=12

How to execute this sql file with dynamic value in Heroku PSQL

I also tried below query:

heroku pg:psql --app application_name <./somepath/file_to_execute.sql --v param1="'$File_name'" --v param2="'$Tag_id'" --v param3="'$job_name'" --v param4="$id"

Got below error message:

Error: Unexpected arguments: param1='file_name.sql', --v, param2='1.1.1', --v, param3='test-name', --v, param4=12

fphilipe
10.1k1 gold badge42 silver badges55 bronze badges
asked Jul 10, 2019 at 11:50

1 Answer 1

0

You can get the URL to the database with the following heroku command:

$ heroku pg:credentials:url --app application_name

This will print something like:

Connection information for default credential.
Connection info string:
 "dbname=xyz host=something.compute.amazonaws.com port=1234 user=foobar password=p422w0rd sslmode=require"
Connection URL:
 postgres://foobar:[email protected]:1234/xyz

The URL (last line) can be used directly with psql. With grep we can get that line and pass it to psql:

$ psql $(heroku pg:credentials:url --app application_name | grep 'postgres://') \
 < ./somepath/file_to_execute.sql \
 -v param1="$File_name" \
 -v param2="$Tag_id" \
 -v param3="$job_name" \
 -v param4="$id"

Note that rather than putting single quotes in the parameter on the command line, you should access the parameter in the SQL as :'param1'.

answered Jul 10, 2019 at 12:23
Sign up to request clarification or add additional context in comments.

Thanks for the reply. I will try this and confirm.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.