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.

Error: syntax error at or near "$1" with super simple query

See original GitHub issue

any query using .query(string, [value...], fn) gives me this error, even with the appropriate number of values, did the api change or something? Even simple stuff like:

    client.query('create user $1 with password \'Something1\'', ['tobi'], function(err){
      if (err) throw err;
      console.log('created');
    });

I’m probably doing something obvious wrong 😄

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

11reactions
brianccommented, Mar 15, 2014

@visionmedia It’s always fun the first time you realize PostgreSQL server doesn’t accept parameters in certain places where you’d love to have them be used. Most commands don’t accept parameters and a few places in queries you think it would be nice to use them they aren’t accepted. 😦

The good news is there are manual escaping helpers already built into node-postgres as well for the times when you want to insert user created or untrusted content into a part of a query or command where parameters aren’t accepted.

https://github.com/brianc/node-postgres/blob/master/lib/client.js#L228 https://github.com/brianc/node-postgres/blob/master/lib/client.js#L247

I apologize these aren’t better documented.

If you’re using the native bindings they actually use the escape written into libpq. If you’re using the pure JavaScript client the escape is as close to a direct port of the libpq escape functions as possible.

1reaction
tjcommented, Mar 17, 2014

https://github.com/segmentio/pg-escape should be sufficient 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting 'error: syntax error at or near...' in Postgresql insert query
But I'm kind of stuck at the most basic insert query which is throwing an error. I have three tables, posts , authors...
Read more >
SQL Error: "Syntax error at or near:" - Looker Community
This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: ... In some circumstances, the database ......
Read more >
syntax error at or near "AS" [closed] - DBA Stack Exchange
I have a program that takes in one or more than one table names, from a list stored in a reference table and...
Read more >
Postgre SQL errors – common codes and messages - Paessler
Here you'll find a list of the most common PostgreSQL errors and proven quick fix solutions: PostgreSQL error “Syntax error at or near...
Read more >
MySQL 1064 Error: You have an error in your SQL syntax
One of the most common causes for the 1064 error is when a SQL statement uses a mistyped command. This is very easy...
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