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