question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I use prepared statements in postgres?

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Hi, I want to use prepared statements in a project. Therefor I am creating an statement and later use this one. However I am getting an error that the parameters/name does not match.

queryRunner.query(
            `PREPARE MY_STATEMENT (text, text, timestamp with time zone, jsonb) as INSERT INTO DUMMY_TABLE (trip_id, vehicle_id, expiry_time, message) VALUES ($1, $2, $3, $4) ON CONFLICT (trip_id) DO UPDATE SET vehicle_id = $2, expiry_time = $3, message = $4`
        );

then in the code I am using the same queryRunner like this:

queryRunner.query("EXECUTE MY_STATEMENT ($1, $2, $3, $4)", ["tripId", "v-id", moment(), null]);

When I am running the code I am getting the following error:

error: Query failed. error: bind message supplies 4 parameters, but prepared statement "" requires 0 - EXECUTE MY_STATEMENT ($1, $2, $3, $4)
(node:58125) UnhandledPromiseRejectionWarning: QueryFailedError: bind message supplies 4 parameters, but prepared statement "" requires 0
    at new QueryFailedError (/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at Query.callback (node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
    at Query.handleError (/node_modules/pg/lib/query.js:142:17)
    at Connection.connectedErrorMessageHandler (/node_modules/pg/lib/client.js:211:17)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (node_modules/pg/lib/connection.js:126:12)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

I am using pg version 7.12.1

My server runs Postgres 10. Can someone please help me?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
jelnurcommented, Jul 10, 2021

const sql = 'SELECT * FROM table_name WHERE col_name = $1'; const result = await conn.manager.query(sql, [ param1 ]);

0reactions
imnotjamescommented, Oct 6, 2020

For stored procedures it gets weird. You’ll need to consult the postgres docs on this - we send everything over to postgres and it figures out what to do.


For other questions, please check out the community slack or check TypeORM’s documentation page on other support avenues - cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: PREPARE
PREPARE creates a prepared statement. A prepared statement is a server-side object that can be used to optimize performance. When the PREPARE statement...
Read more >
PostgreSQL prepared statements in PL/pgSQL
This is much slower because the implicit prepared statement is not used here. The statement is parsed each time. Let me prepare it...
Read more >
What is the difference between prepared statements and ...
Thanks. PostgreSQL document says that "When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. When ...
Read more >
Prepared Statements | Npgsql Documentation
Note that all parameters must be set before calling Prepare() - they are part of the information transmitted to PostgreSQL and used to...
Read more >
Server Prepared Statements
Server Prepared Statements · Motivation. The PostgreSQL server allows clients to compile sql statements that are expected to be reused to avoid the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found