N
Neon11mo ago
stormy-gold

ECONNRESET error when trying to create a database from a javascript file

So I'm receiving the below error when trying to create a table from a js file using postgres. In the annexed images you can see how I wrote the code for creating the table. I was following a video doing the same, for him it worked, but not for me. The error doesn't happen if I use async, but the table doesn't get created either. What's happening here and how to solve it?
Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
at cachedError (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/query.js:170:23)
at new Query (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/query.js:36:24)
at sql (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/index.js:112:11)
at file:///C:/Users/bruno/projetos/node/node-do-zero/create-videos-table.js:3:4 {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
at cachedError (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/query.js:170:23)
at new Query (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/query.js:36:24)
at sql (file:///C:/Users/bruno/projetos/node/node-do-zero/node_modules/postgres/src/index.js:112:11)
at file:///C:/Users/bruno/projetos/node/node-do-zero/create-videos-table.js:3:4 {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
No description
No description
7 Replies
conscious-sapphire
conscious-sapphire11mo ago
Hey @Amodeus R. this is odd - that SQL you are using is MySQL syntax. PostgreSQL has no "ON UPDATE" - see https://www.morling.dev/blog/last-updated-columns-with-postgres/
Last Updated Columns With Postgres
In many applications it’s a requirement to keep track of when a record was created and updated the last time. Often, this is implemented by having columns such as created_at and updated_at within each table. To make things as simple as possible for application developers, the database itself should take care of maintaining these values automati...
conscious-sapphire
conscious-sapphire11mo ago
if I remove the ON UPDATE CURRENT_TIMESTAMP I am able to run your exact code fine. but your error seems to indicate something else on your system is misconfigured
stormy-gold
stormy-goldOP11mo ago
oh I see, so I got trolled by ChatGPT then... I'll try fixing that then, I'll say if I it worked for me, thanks for the answer!
conscious-sapphire
conscious-sapphire11mo ago
classic ChatGPT gaslighting
stormy-gold
stormy-goldOP11mo ago
Unfortunately just changing the syntax didn't make it work, but what else could be misconfigured? Isn't all what I show the necessary to make it work?
conscious-sapphire
conscious-sapphire11mo ago
the error is coming from the postgres.js module - there is a whole stack of dependencies below your code that could be causing the error... What version of Node are you using? node -v ? What version of postgres.js is listed in your package.json file? oh you know what - one other thing I just noticed. You may need to: 1. npm install dotenv 2. Add the init to your db.js file
import dotenv from 'dotenv';
dotenv.config();
import dotenv from 'dotenv';
dotenv.config();
stormy-gold
stormy-goldOP11mo ago
Indeed it had to do with the dotenv file. I was trying to use the built-in dotent from nodejs, but it wasn't working. Installing the dotenv dependency fixed it, thanks for the help :D

Did you find this page helpful?