Skip to content
Advertisement

Getting ‘error: syntax error at or near…’ in Postgresql insert query

I’m fairly new at Postgresql and learning new things everyday. So I have this blog project where I want to use PostgreSQL as a db. But I’m kind of stuck at the most basic insert query which is throwing an error. I have three tables, posts, authors and categories. I could create the table properly I guess but when I try to insert data I get this error:

Now I don’t know where the issue is and Postgres’ errors are not that specific.

Can anyone please tell me where I could be going wrong?

Here are the tables:

Here’s the async function where I’m making the insert query:

UPDATE————————————–

This is how the Postgres log file looks like:

Advertisement

Answer

Your string values are not quoted. It would have to be…

You could add quotes to your query, but don’t. Your query as written is insecure and vulnerable to a SQL injection attack. Do not insert values into queries with string concatenation.

Instead, use parameters.

Postgres will handle the quoting for you. This is safer, more secure, and faster.


Note that an3cxZh8ZD3tdtqG4wuwPR is not a valid UUID. A UUID is a 128 bit integer often represented as a 32 character hex string.

Note that you also probably want to use autoincrementing primary keys instead of generating the ID yourself. For a UUID primary key, load the uuid-ossp package and use its UUID function as your default.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement