node-postgres 是用于与你的 PostgreSQL 数据库交互的 node.js 模块的集合。它支持回调、promise、异步/等待、连接池、准备好的语句、游标、流式结果、C/C++ 绑定、丰富的类型解析等等!就像 PostgreSQL 本身一样,它有很多功能:本文档旨在让你快速启动并朝着正确的方向前进。它还尝试为更高级和边缘情况的主题提供指南,让你可以从 node.js 充分利用 PostgreSQL 的全部功能。
¥node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database. It has support for callbacks, promises, async/await, connection pooling, prepared statements, cursors, streaming results, C/C++ bindings, rich type parsing, and more! Just like PostgreSQL itself there are a lot of features: this documentation aims to get you up and running quickly and in the right direction. It also tries to provide guides for more advanced & edge-case topics allowing you to tap into the full power of PostgreSQL from node.js.
安装
¥Install
$ npm install pg
支持者
¥Supporters
node-postgres 的持续开发和支持得益于众多 supporters。
¥node-postgres continued development and support is made possible by the many supporters.
如果你或你的公司想要赞助 node-postgres,请到 GitHub 赞助商 并注册,或者如果你想要在文档中添加你的徽标或讨论更高级别的赞助,请随时到 给我发电子邮件!
¥If you or your company would like to sponsor node-postgres stop by GitHub Sponsors and sign up or feel free to email me if you want to add your logo to the documentation or discuss higher tiers of sponsorship!
版本兼容性
¥Version compatibility
node-postgres 致力于与所有最新的 LTS 版本的节点和最新的 "stable" 版本兼容。在撰写本文时,node-postgres 与 node 8.x、10.x、12.x 和 14.x 兼容。要使用 node >= 14.x,你需要安装 pg@8.2.x
或更高版本,因为 node 14 分支上的一些内部流发生了变化。放弃对旧节点 lts 版本的支持将始终被视为 node-postgres 中的重大更改,并且只会在主要版本号更改时进行,我们将尝试尽可能长时间地保持对 8.x 的支持。
¥node-postgres strives to be compatible with all recent LTS versions of node & the most recent "stable" version. At the time of this writing node-postgres is compatible with node 8.x, 10.x, 12.x and 14.x To use node >= 14.x you will need to install pg@8.2.x
or later due to some internal stream changes on the node 14 branch. Dropping support for an old node lts version will always be considered a breaking change in node-postgres and will be done on major version number changes only, and we will try to keep support for 8.x for as long as reasonably possible.
入门
¥Getting started
连接、查询和断开连接的最简单方法是使用 async/await:
¥The simplest possible way to connect, query, and disconnect is with async/await:
import pg from 'pg'
const { Client } = pg
const client = new Client()
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()
错误处理
¥Error Handling
为了简单起见,这些文档将假设这些方法是成功的。在实际使用中,请确保正确处理方法中抛出的错误。try/catch
块是一种很好的方法:
¥For the sake of simplicity, these docs will assume that the methods are successful. In real life use, make sure to properly handle errors thrown in the methods. A try/catch
block is a great way to do so:
import pg from 'pg'
const { Client } = pg
const client = new Client()
await client.connect()
try {
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
} catch (err) {
console.error(err);
} finally {
await client.end()
}
回调
¥Callbacks
如果你更喜欢回调式异步编程方法,则所有异步方法也支持可选的回调参数:
¥If you prefer a callback-style approach to asynchronous programming, all async methods support an optional callback parameter as well:
import pg from 'pg'
const { Client } = pg
const client = new Client()
client.connect((err) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
console.log(err ? err.stack : res.rows[0].message) // Hello World!
client.end()
})
})
我们现实世界中的应用几乎总是比这更复杂,我建议你继续阅读!
¥Our real-world apps are almost always more complicated than that, and I urge you to read on!