Answer the question
In order to leave comments, you need to log in
How to connect to postgreSQL on openserver from node.js?
Hello everyone, I'm a beginner, I'm learning to develop a backend on a node, I created a database on postgreSQL through an open server, it shows the version of node.js in the open server console, please tell me the next steps to connect from a node?
My actions - I start the server on host 3000, I make a request from the postman, I get an error from krakozyabr in the console, nothing is clear.
At the moment the code is like this, what am I doing wrong?
Connection:
const Pool = require('pg').Pool
const pool = new Pool({
user: 'postgres',
password: '',
host: 'localhost',
port: 5432,
database: 'testable'
});
module.exports = pool;
const express = require('express');
const app = express();
const pool = require('./db');
const PORT = process.env.PORT || 3000;
app.use(express.json())
app.post('/userdata', async (req, res) => {
try {
const { username } = req.body
// console.log(req.body)
const newUser = await pool.query(
`INSERT INTO testable (username) VALUES ($1) RETURNING *`,
[username]
)
res.json(newUser)
} catch (err) {
console.error(err + ' ошибка');
}
})
app.listen(PORT, () => console.log(`Server started on post ${PORT}`));
Answer the question
In order to leave comments, you need to log in
An interesting feature of the open server is that in the name of the database you need to indicate not the table you are working with, but postgres, and in the queries you already indicate the name of the table
const Pool = require('pg').Pool
const pool = new Pool({
user: 'postgres',
password: 'postgres',
host: 'localhost',
port: 5432,
database: 'postgres' // тут
});
module.exports = pool;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question