官方文档
nodepostgres是node.js模块的集合,用于与PostgreSQL数据库接口。它支持回调、promise、async/await、连接池、准备好的语句、游标、流式结果、C/C++绑定、富类型解析等等!就像PostgreSQL本身一样,它有很多功能:本文档旨在让您快速、正确地运行。它还试图为更高级和边缘案例主题提供指南,使您能够从node.js充分利用PostgreSQL的功能。
npm install pg
import { Client } from 'pg'
const client = new Client({
host: 'my.database-server.com',
port: 5334,
database: 'database-name',
user: 'database-user',
password: 'secretpassword!!',
})
await client.connect()
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()
"err":{"type":"DatabaseError","message":"column \"xxx报错点xxx\" does not exist",
遇到这种报错,八成是你的sql句子拼的有问题,仔细检查去吧
举个栗子:
const insertApp = await client.query(`INSERT INTO applications
(app_name )
VALUES
('${validResult.appName}')
ON CONFLICT (app_name)
DO NOTHING;
`);
const text = 'INSERT INTO users(name, email) VALUES($1, $2) RETURNING *'
const values = ['brianc', 'brian.m.carlson@gmail.com']
const res = await client.query(text, values)
欢迎大家指出文章需要改正之处~
学无止境,合作共赢