bwscheddebrock_trassenplaner/list_all_schemas.js

33 lines
1.0 KiB
JavaScript

const { Pool } = require('pg');
require('dotenv').config();
const pool = new Pool({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
async function listAllSchemas() {
try {
const res = await pool.query("SELECT schema_name FROM information_schema.schemata");
console.log("Schemas in database:");
res.rows.forEach(row => console.log("- " + row.schema_name));
for (const schemaRow of res.rows) {
const schema = schemaRow.schema_name;
if (schema.startsWith('pg_') || schema === 'information_schema') continue;
const tablesRes = await pool.query(`SELECT table_name FROM information_schema.tables WHERE table_schema = $1`, [schema]);
console.log(`Tables in schema ${schema}:`);
tablesRes.rows.forEach(t => console.log(` - ${t.table_name}`));
}
} catch (err) {
console.error("Error:", err);
} finally {
await pool.end();
}
}
listAllSchemas();