node-postgres 创建数据库 [英] node-postgres create database
问题描述
我正在使用 node-postgres,并且在我的应用程序开始时我想检查数据库是否存在或不是.所以我的工作流程想法如下:
I am using node-postgres, and at the beginning of my application I want to check whether the database exists or not. So my workflow idea is as following:
- 检查
myDb
是否存在 - 如果存在,则创建表
- 如果没有,则先创建数据库,然后创建表
如您所见,这是一个非常简单的过程,但是,驱动程序实现需要有一个数据库名称 postgres://username:password@host/database
才能连接,这意味着您需要首先连接到数据库.
As you see it is a really easy process, however, the driver implementation requires to have a database name postgres://username:password@host/database
to be connected, which means you need to connect to a database first.
所以我现在要做的是在开始时连接到 postgres
数据库,进行查询以创建数据库,如果异常已经存在,则将其捕获,然后关闭我的连接并连接到新创建的数据库,然后创建表.代码如下:
So what I am doing now is to connect to postgres
database at the beginning, making a query to create database, cathing the exception if it is already there, then closing my connection and connecting to the newly created database, then creating the tables. Here is the code:
var conStringPri = 'postgres://' + username + ':' + password + '@' + host +
'/postgres';
var conStringPost = 'postgres://' + username + ':' + password + '@' + host +
'/' + dbName;
pg.connect(conStringPri, function(err, client, done) { // connect to postgres db
if (err)
console.log('Error while connecting: ' + err);
client.query('CREATE DATABASE ' + dbName, function(err) { // create user's db
if (err)
console.log('ignoring the error'); // ignore if the db is there
client.end(); // close the connection
// create a new connection to the new db
pg.connect(conStringPost, function(err, clientOrg, done) {
// create the table
clientOrg.query('CREATE TABLE IF NOT EXISTS ' + tableName + ' ' +
'(...some sql...)';
});
});
});
如您所见,我打开和关闭连接两次,这种方式对我来说似乎是错误的.如果您提出更好的方法,或者解释一下您是如何做到的,我会很高兴.
As you see I am opening and closing the connection twice, and this way seems wrong to me. I'll be glad if you propose a better way, or maybe explain how did you accomplish this.
推荐答案
正如您所见,这是一个非常简单的过程,但是,驱动程序实现需要有一个数据库名称postgres://username:password@host/database 要连接,其中意味着您需要先连接到数据库.
As you see it is a really easy process, however, the driver implementation requires to have a database name postgres://username:password@host/database to be connected, which means you need to connect to a database first.
这不是因为驱动程序的实现,而是 PostgreSQL 本身.任何其他语言或驱动程序都一样.
It's not because of the driver implementation, it's PostgreSQL itself. It's the same with any other language or driver.
客户端需要连接到数据库才能执行任何操作,包括CREATE DATABASE
.除了postgres
数据库,template1
也经常用于此目的.
A client needs to be connected to a database in order to do anything, including a CREATE DATABASE
. Besides the postgres
database, template1
is often used for this purpose too.
然后,由于您必须连接到新创建的数据库才能在其中创建对象,因此无法避免打开另一个连接.
Then, since you must connect to the freshly created database to create objects inside it, there's no way to avoid opening another connection.
简而言之,您所做的事情不能简化,它已经是最佳的.
In short, what you're doing can't be simplified, it's already optimal.
这篇关于node-postgres 创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!