Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

409
Views
How do you run psql commands in js

I'm trying to run psql commands in js. The objective would be to run commands like \c <database_name> or \i 'file.sql'. I understand that there is a js library that allows you to connect to postgres (called pg). However it seems like it only connects to a database in postgres and it doesn't seem to allow me to run psql commands. For example I have:

import { Client } from 'pg';

export async function someFunction() {
    const client = new Client({
        user: 'postgres',
        host: 'localhost',
        database: 'testDB',
        password: 'postgres',
        port: 5432
    });
    client.connect();

    // this should populate my db given the test.sql file
    const res = await client.query(`\i ./test.sql`);
    console.log(res.rows);

    // This should return me the list of tables in the connected db
    const test = await client.query('\\dt public.*');
    console.log("test: ", test);

    await client.end();
}

Even though (given this docs) I should be populating my db (line const res = await client.query(\i ./test.sql);), I instead get this error: error: syntax error at or near "\".
How may I run these commands in js (or in my case ts).

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!