Access Retool Database directly from Javascript

It is possible to directly access the Retool Database via a JS function or JS block, or is the only way to interact with the DB data via a query?

For instance can you use node-postgres to interact directly with the db? Ideally Retool would manage the authentication or allow access to the a client with access to the databse.

I suspect this is not possible, but just wanted to confirm. If someone does have it working could they share a sample of their code?

An example of how this might look is below

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

const client = new Client({
    host: 'your_database_host',
    port: 5432,
    user: 'your_database_user',
    password: 'your_password',
    database: 'your_database_name'
});

async function fetchData() {
    await client.connect();

    const res = await client.query('SELECT * FROM your_table_name');
    console.log(res.rows);

    await client.end();
}

fetchData();

Hello, welcome to the community.
Yes, you can.

I connect it just now.
image
You can get connect information here.
image

index.js

import pkg from 'pg';

const {  Client, Pool } = pkg;
 

const connectionString = 'postgresql://production:*******@*****.retooldb.com/production';
 
const pool = new Pool({
  connectionString,
})
 
await pool.query('SELECT NOW()')
 console.log(await pool.query('SELECT * FROM users'))
await pool.end()

const client = new Client({
  connectionString,
})
 
await client.connect()
 
await client.query('SELECT NOW()')
 
await client.end()

package.json

{
  "type": "module",
  "dependencies": {
    "pg": "^8.11.3"
  }
}
1 Like