Using node-postgres to Connect to

First, install node-postgres for your node project.

npm install pg

Then sign in to and click "Connect" to get your connection info.

Using that information, you can connect to and query your databases or public databases hosted on Try copying the code below and swapping out the connection information.

const { Pool } = require('pg');

// Create a connection pool using the connection information provided on
const pool = new Pool({
    user: 'GettingStarted',
    host: '',
    database: 'dliden/2020_Census_Reapportionment', // public database 
    password: '<bitio_key>', // key from database page connect menu
    port: 5432,
    ssl: true,

pool.query('SELECT * FROM "dliden/2020_Census_Reapportionment"."Historical Apportionment" limit 10;', (err, res) => {
    console.table(res.rows); // you could also just console.log, but console.table is neat :)

Now run the program using node and you should see the rows you requested printed out in the console.

node main.js


Error: Connection terminated unexpectedly — Use a connection pool

It is recommended to use a node-postgres connection pool with (as shown in the connection snippet above). automatically closes idle connections (after 5 minutes, at the time of writing) and the connection pool interface will automatically handle opening new connections as needed.

In addition, if you explicitly checkout a client from a node-postgres pool using the pool's connect method, you must return the client to the pool using the client's restore method when you are not using it for 5 minutes or more. Otherwise, you may still experience connection timeout errors.

To learn more about using your Postgres database with node.js, read the node-postgres docs.