logoDenoby example

Intermediate

clideploy

Connect to Postgres

Edit

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

Import the Client constructor from deno.land/x
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
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 https://examples.deno.land/postgres.ts