1

In PostgreSQL, is it possible to parameterize the table name used in a query?

2
  • No (and not only in Postgres is it not possible). You need dynamic SQL for this. Commented Dec 30, 2020 at 0:56
  • We need more context as some libraries like Python's pyscopg2 does support escaping identifiers like table names. Commented Dec 30, 2020 at 0:59

1 Answer 1

1

Think of parameters as a replacement for scalar values only. Use one parameter in the place where you could use one string literal or numeric literal.

You cannot use parameters for other parts of an SQL query:

  • Identifiers like table names, column names, etc.
  • SQL keywords
  • Expressions
  • Lists of values, such as in an IN (...) predicate. Each value in the list would need an individual parameter.

All those parts of the SQL query must be fixed by the time the query is parsed during prepare(). If a client library supports "parameters" for identifiers, it's really doing string-interpolation into the SQL query before the query is parsed.

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.