Sample Header Ad - 728x90

How to pass parameters to sql script via psql \i command

3 votes
1 answer
13883 views
The psql \i command is able to execute a given SQL script but I need a way to pass parameters to the script. Example: say you have this simple script
select * from :table LIMIT 1;
I've tried
my_db=> \i my-script.sql -v table="core.product"
but got this error
psql:my-script.sql:1: ERROR:  syntax error at or near ":"
LINE 1: select * from :table LIMIT 1;
                      ^
\i: extra argument "-v" ignored
\i: extra argument "table="core.product"" ignored
--- I know that running this on terminal will work, but I'm already inside psql.
psql -v table="core.product" -f my-script.sql
Asked by Michael Pacheco (133 rep)
Sep 28, 2021, 06:38 PM
Last activity: Sep 28, 2021, 07:38 PM