英文:
How to create a database using libpq-fe
问题
我正在尝试使用C代码和libpq-fe.h创建一个新的数据库。
问题是,互联网上没有一个示例可以告诉你如何做到这一点,包括文档。
如果我尝试在连接中指定要创建的数据库名称,我会收到错误消息,如果不指定,我认为它会自动连接到postgres数据库。
但我想要创建并连接到一个新的数据库,我该如何做呢?
英文:
I'm trying to create a new database using C code and libpq-fe.h
The problem is taht there's literally not a single example on the internet to show you how to do so, including the docs.
If I try to specify the database name that I want to create in the connection I get an error, if I don't, I think it connects automatically to postgres database.
But I want to create and connect to a new database, how can I do that?
答案1
得分: 1
唯一创建数据库的方法是首先连接到另一个数据库,然后在那里运行 CREATE DATABASE
。这就是 postgres
数据库的作用。因此,请执行以下步骤:
- 连接到数据库
postgres
- 执行
CREATE DATABASE
语句 - 从
postgres
断开连接 - 连接到新创建的数据库
英文:
The only way to create a database is to first connect to another database, then run CREATE DATABASE
there. That's what the postgres
database is there for. So do this:
-
connect to database
postgres
-
execute the
CREATE DATABASE
statement -
disconnect from
postgres
-
connect to the newly created database
答案2
得分: 0
Sure, here are the translations:
"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:
any particular reason you want to create the database from the app?<br> -> 你想从应用程序创建数据库的特定原因吗?
I would use dBeaver, best tool for database manipulations.<br> -> 我会使用dBeaver,这是最好的数据库操作工具。
https://dbeaver.io/download/ -> https://dbeaver.io/download/
anyways have you looked at this:<br> -> 无论如何,你看过这个吗:
https://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm<br> -> https://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
I'd connect using that and then create a database regularly with an SQL command:<br> -> 我会使用它进行连接,然后定期使用SQL命令创建数据库:
https://www.postgresql.org/docs/current/sql-createdatabase.html -> https://www.postgresql.org/docs/current/sql-createdatabase.html"
英文:
any particular reason you want to create the database from the app?<br>
I would use dBeaver, best tool for database manipulations.<br>
https://dbeaver.io/download/
anyways have you looked at this:<br>
https://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm<br>
I'd connect using that and then create a database regularly with an SQL command:<br>
https://www.postgresql.org/docs/current/sql-createdatabase.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论