Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I quote a named argument passed in to psql?

psql has a construct for passing named arguments:

psql -v name='value'

which can then be referenced inside a script:

SELECT :name;

which will give the result

 ?column?
----------
 value
(1 row)

During development, I need to drop and recreate copies of the database fairly frequently, so I'm trying to automate the process. So I need to run a query that forcibly disconnects all users and then drops the database. But the database this operates on will vary, so the database name needs to be an argument.

The problem is that the query to disconnect the users requires a string (WHERE pg_stat_activity.datname = 'dbname') and the query that drops requires an unquoted token (DROP DATABASE IF EXISTS dbname). (Sorry. Not sure what to call that kind of token.)

I can use the named argument fine without quotes in the DROP query, but quoting the named argument in the disconnect query causes the argument to not be expanded. I.e., I would get the string ':name' instead of the string 'value'.

Is there any way to turn the unquoted value into a string or turn a string into an unquoted token for the DROP query? I can work around it by putting the disconnect and DROP queries in separate scripts and passing the argument in with quotes to the disconnect and without quotes to the DROP, but I'd prefer they were in the same script since they're really two steps in a single process.

like image 792
jpmc26 Avatar asked Aug 15 '12 16:08

jpmc26


1 Answers

Use:

... WHERE pg_stat_activity.datname = :'name'

Note the placement of the colon before the single quote.
The manual:

If an unquoted colon (:) followed by a psql variable name appears within an argument, it is replaced by the variable's value, as described in SQL Interpolation below. The forms :'variable_name' and :"variable_name" described there work as well.

And:

To quote the value of a variable as an SQL literal, write a colon followed by the variable name in single quotes.

like image 153
Erwin Brandstetter Avatar answered Sep 29 '22 06:09

Erwin Brandstetter