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;
^
To create a PostgreSQL view, we use the CREATE VIEW statement. Here is the syntax for this statement: CREATE [OR REPLACE] VIEW view-name AS SELECT column(s) FROM table(s) [WHERE condition(s)]; The OR REPLACE parameter will replace the view if it already exists.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With