0

**我使用 Nodejs 连接到 Postgres 服务器,现在我想查看连接服务器下的所有数据库。请帮我 **

4

2 回答 2

4

请参阅下面的mysql数据库:

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);
  });
});

于 2020-01-07T07:32:25.660 回答
1

SQL

SELECT datname FROM pg_database
WHERE datistemplate = false;

例子

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() }
)
于 2020-01-07T07:31:41.253 回答