连接到 Postgres
在 Github 上编辑
使用 npm Postgres 客户端,您可以连接到任何地方运行的 Postgres 数据库。
从以下位置导入 Postgres 包
import postgres from "npm:postgres";
使用数据库的连接信息初始化客户端,并创建连接。
const sql = postgres({
user: "user",
database: "test",
hostname: "localhost",
port: 5432,
});
执行 SQL 查询
const result = await sql`
SELECT ID, NAME FROM PEOPLE
`;
console.log(result);
关闭与数据库的连接
await sql.end();
使用 Deno CLI 在本地运行此示例
deno run --allow-net --allow-env https://docs.deno.org.cn/examples/scripts/postgres.ts