0

I'm new to node-postgres and trying to pass in parameters connected to a table alias, but it's not working. How can I parameterize the simple query below? Take something like

const pgQuery = 'SELECT t1.YEAR as year, t1.CODE as code FROM data t1;';

and instead do something like

const pgQuery = 'SELECT t1.$1 as year, t1.$2 as code FROM data t1;';
const values = ['YEAR', 'CODE'];

1 Answer 1

1

Per the docs here:

https://node-postgres.com/features/queries

PostgreSQL does not support parameters for identifiers. If you need to have dynamic database, schema, table, or column names (e.g. in DDL statements) use pg-format package for handling escaping these values to ensure you do not have SQL injection!

Which takes you here:

https://www.npmjs.com/package/pg-format

Repo:

https://github.com/datalanche/node-pg-format

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

Comments

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.