Satish0302
Satish0302

Reputation: 158

How can I see all the databases of connected server in nodejs postgres/mysql

**I connected to the Postgres server using Nodejs and now I want to see all the databases under the connected server. Please help me **

Upvotes: 1

Views: 3020

Answers (2)

Ankur Munjaal
Ankur Munjaal

Reputation: 87

Please see below for mysql database:

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  con.query("show databases", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

Upvotes: 5

user1123335
user1123335

Reputation:

SQL

SELECT datname FROM pg_database
WHERE datistemplate = false;

Example

const { Pool, Client } = require('pg')
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'password',
port: 5432,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const client = new Client({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
})
client.connect()
client.query('SELECT datname FROM pg_database WHERE datistemplate = false;', (err, res) => { console.log(err, res) client.end() }
)

Upvotes: 1

Related Questions