Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
How can I use a variable as the table name in node-pg?

For example I have a query like:

      const rows = await db.query(
        "SELECT * FROM $1 WHERE email = $2 AND password = $3",
        [tableName, email, password]
      );

It gives me a syntax error.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is not possible, parameterised queries only work for values (instead of literals) but not for identifiers. You will need to build the SQL string:

const rows = await db.query(
  `SELECT * FROM ${db.escapeIdentifier(tableName)} WHERE email = $1 AND password = $2`,
  [email, password]
);

(Assuming the db is a PgClient)

If you know the possible values of the tableName variable beforehand, you might get away without escaping; if you don't, you better also specify the schema explicitly.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!