node-postgres README

The project's README from github.com/brianc/node-postgres. As one markdown file: README.md.

node-postgres

Build Status NPM version NPM downloads

Non-blocking PostgreSQL client for Node.js (and bun, deno, cloudflare, etc...). Pure JavaScript and optional native libpq bindings.

Monorepo

This repo is a monorepo which contains the core pg module as well as a handful of related modules.

Install

npm install pg

Documentation

Each package in this repo should have its own readme more focused on how to develop/contribute. For overall documentation on the project and the related modules managed by this repo please see:

:star: Documentation :star:

The source repo for the documentation is available for contribution here.

Features

Extras

node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture. The entire list can be found on our wiki.

Support

node-postgres is free software. If you encounter a bug with the library please open an issue on the GitHub repo. If you have questions unanswered by the documentation please open an issue pointing out how the documentation was unclear & I will do my best to make it better!

When you open an issue please provide:

You can also follow me @brianc on bluesky if that's your thing for updates on node-postgres with nearly zero non node-postgres content. My old twitter/x account is no longer used.

Sponsorship :two_hearts:

node-postgres's continued development has been made possible in part by generous financial support from the community.

If you or your company are benefiting from node-postgres and would like to help keep the project financially sustainable please consider supporting its development.

Special thanks to medplum for their generous and thoughtful support of node-postgres!

Medplum logo

Contributing

:heart: contributions!

I will happily accept your pull request if it:

If your change involves breaking backwards compatibility please please point that out in the pull request & we can discuss & plan when and how to release it and what type of documentation or communication it will require.

Setting up for local development

  1. Clone the repo
  2. Ensure you have installed libpq-dev in your system (the native bindings are built in the test process)
  3. From your workspace root run yarn and then yarn lerna bootstrap
  4. Ensure you have a PostgreSQL instance running with SSL enabled and an empty database for tests. note: you can skip the tests requring SSL by setting the environment variable PGTESTNOSSL=1 if you're not changing any SSL related code.
  5. Ensure you have the proper environment variables configured for connecting to your postgres instance. Using the standard PG* environment variables like PGUSER and PGPASSWORD etc...
  6. Run yarn test to run all the tests.

Troubleshooting and FAQ

The causes and solutions to common errors can be found among the Frequently Asked Questions (FAQ)

License

Copyright (c) 2010-2020 Brian Carlson ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-postgres (brianc/node-postgres)

Searching Database…

这是干什么的?node-postgres(pg)是一个用于 Node.js 的非阻塞 PostgreSQL 客户端库——纯 JavaScript 实现,可选地使用原生 libpq 绑定。你的 JS 代码可以用它连接 PostgreSQL 数据库、执行查询并流式读取数据。
这是个独立应用,还是我要加到自己代码里的东西?它是一个需要安装到你的 JavaScript 项目里的库,不是独立应用。这个仓库是一个 monorepo:核心的 pg 包,加上 pg-pool、pg-cursor、pg-query-stream 等相关模块。
怎么把它加到我的项目里?先从 npm 安装 pg 包(npm install pg),然后用你的连接配置创建一个 client 或 pool,就可以在代码里开始执行查询了。
我需要先安装 libpq 吗?不需要——默认客户端是纯 JavaScript 的;原生 libpq 绑定是可选的。两种实现暴露相同的 API,所以只有在你特别想用原生版本时才需要 libpq。
它只能在 Node.js 下运行吗?Node.js 是主要目标平台,但 README 说它也能在 bun、deno、cloudflare 等环境上运行。它是一个服务端数据库驱动,不是浏览器端的库。
我能在商业项目里用它吗?可以——它是 MIT 许可证下的免费软件,商业使用、修改和再分发都没问题。
这个 monorepo 里除了 pg 还有什么?pg-pool、pg-native、pg-cursor、pg-query-stream、pg-connection-string 和 pg-protocol——它们是独立的包,分别负责连接池、原生绑定、基于游标的读取、流式返回结果、连接字符串解析,以及底层通信协议。
它的性能真的很快吗?README 称它是 Node.js 上最快的 PostgreSQL 客户端。如果想更进一步,可选的原生 libpq 绑定可以直接无缝替换,因为两种实现共用同一套 API。
这个项目成熟吗?非常成熟——它从 2010 年就有了,在 GitHub 上有超过 13k 颗星,是 Node 生态里的中坚项目。持续开发有一部分由社区赞助者资助,medplum 是其中的重点赞助方。
查询结果太多时,我可以流式读取而不是一次性全部加载吗?可以——monorepo 里包含用于流式读取行的 pg-query-stream,以及按批次读取结果的 pg-cursor;两者都是配合 pg 使用的独立 npm 包。
What are similar projects?sqlx, pgx, usql, pglite, crystal, pgterm, neon, parse-server
Creator
brianc
Size
5.3 MB
Stars
13.2K
Updated
CommitSep 30
Age
16y
Commits · 90 Days