问题是:
使用Node.js包'pg-promise',我似乎无法弄清楚是否可以创建新数据库。我可以连接到现有的数据库,没有任何问题。但是,当我没有指定数据库名称运行时,就会出现错误:
这是我正在尝试的内容:
"jeff" 是我的 Ubuntu 登录名。
1) Can pg-promise be used to create a new database (schemas, et. al.)?
使用Node.js包'pg-promise',我似乎无法弄清楚是否可以创建新数据库。我可以连接到现有的数据库,没有任何问题。但是,当我没有指定数据库名称运行时,就会出现错误:
这是我正在尝试的内容:
host = host || '127.0.0.1'
port = port || '5432'
const pgp = require('pg-promise')(pgpInitOptions)
// database connection details
const pgConnectionOptions = {
host: host,
port: port,
user: user,
password: pass
}
// database instance
const localDB = pgp(pgConnectionOptions)
let escapedDbName = dbName.replace(/\"/g, '""');
let sql = 'CREATE DATABASE "' + escapedDbName + '"';
localDB
.any(sql)
.then((results) => {
console.log('creation of database successful', results)
})
.catch((error) => {
console.error('creation of database failed', error)
})
然后,我遇到了这个错误:
creation of database failed
Error: database "jeff" does not exist
"jeff" 是我的 Ubuntu 登录名。