I want to create a database and a table in that database.
我想创建一个数据库并在该数据库中创建一个表。
I create a new database and then a new table "users".
But the error "relation already exists" appears.
Yes, I already have a table "users", but it exists in another database.
But I want this table in the newly created database.
我创建了一个新的数据库,然后创建了一个新的表“users”。但出现错误“关系已存在”。是的,我已经有一个表“users”,但它存在于另一个数据库中。但是我希望这个表在新创建的数据库中。
import pkg from 'pg'; // node-postgres
const { Client } = pkg;
const client = new Client({
host: ***,
user: ***,
password: ***,
});
await client.connect();
await client.query(`
CREATE DATABASE book_review_app;
`);
await client.query(`
CREATE TABLE USERS (
id SERIAL PRIMARY KEY,
name character varying NOT NULL,
email character varying NOT NULL UNIQUE,
password varchar NOT NULL
);
`);
更多回答
优秀答案推荐
Your client is still connected to the original ***
database, not to the newly created one. You'll need to open a new connection (create a new client) to book_review_app
.
您的客户端仍然连接到原来的*数据库,而不是新创建的数据库。您需要打开一个新的连接(创建一个新的客户端)到book_Review_app。
更多回答
我是一名优秀的程序员,十分优秀!