Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

407
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda