Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I want to run the following parameterized query from the psql command line:

SELECT *
FROM users
WHERE username = :username;

How do I set the username parameter on the command line?

I tried this:

\set username 'john'

But when I run the query, I get the following error message:

ERROR:  column "john" does not exist
LINE 3: WHERE username = john;
                         ^
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Per the psql documentation, to substitute a psql variable into a string as a literal, use :'variablename'

This isn't really a parameterised query in the usual sense, as the variable is interpolated into the query string. psql knows to escape single quotes, though, so a variable value ');DROP TABLE users;-- will appear literally instead of ending the string and running unwanted SQL.

share|improve this answer
    
Awesome! Thanks for clarifying. – Naresh Nov 25 at 1:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.