11

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;
                         ^

1 Answer 1

8

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.

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

1 Comment

Awesome! Thanks for clarifying.

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.