gpt4 book ai didi

How to create a table in a newly created database in PostgreSQL?(如何在PostgreSQL中新建的数据库中创建表?)

翻译 作者:bug小助手 更新时间:2023-10-26 22:14:39 29 4
gpt4 key购买 nike



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。


更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com