连接到 Postgres
使用 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 -N -E https://docs.deno.org.cn/examples/scripts/postgres.ts