Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

411
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda