Connecting via Node.js

First, install node-postgres for your node project.

npm install pg

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

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

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

// Create a client using the connection information provided on bit.io.
const client = new Client({
    user: 'GettingStarted',
    host: 'db.bit.io',
    database: 'dliden/2020_Census_Reapportionment', // public database 
    password: '<bitio_key>', // key from bit.io database page connect menu
    port: 5432,
    ssl: true,
});
client.connect();

client.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

Did this page help you?