Connect to Postgres


Using the Deno Postgres client, you can connect to a Postgres database running anywhere.

Import the Client constructor from
import { Client } from "";
Initialize the client with connection information for your database, and create a connection.
const client = new Client({
  user: "user",
  database: "test",
  hostname: "localhost",
  port: 5432,
await client.connect();
Execute a SQL query
const result = await client.queryArray("SELECT ID, NAME FROM PEOPLE");
console.log(result.rows); // [[1, 'Carlos'], [2, 'John'], ...]
Close the connection to the database
await client.end();

Run this example locally using the Deno CLI:

deno run --allow-net --allow-env