我有一个应用程序,将创建和删除postgres数据库。 应用程序本身有自己的sql server数据库。 有点怪异的建筑,但它不是选择。
我对如何连接到postgres服务器以执行这些create table和drop table命令感到有点困惑。 通常在app.config或web.config ,连接字符串将指定数据库。 在这种情况下,我只想指定服务器。
查询可以直接运行到postgres服务器,没有特定的数据库吗?
我应该使用服务器创建的postgres数据库吗? 我试过这个... select * from pg_database然后从第一个查询中drop database DBNAME ,并且它给出了一个错误,说数据库不存在。
或者我可以创建一个空数据库来连接并向其提交查询,尽管它没有用于任何事情。
I have an application that will be creating and dropping postgres databases. The application itself has its own sql server database. Kind of a bizarre architecture but it's not by choice.
I'm a little confused on how I should connect to the postgres server to execute these create table and drop table commands. Normally in an app.config or web.config, the connection string would specify the database. In this case, I just want to specify the server.
Can queries be run directly to a postgres server, without a particular database?
Should I use the postgres database that was created by the server? I tried this... select * from pg_database and then drop database DBNAME with a result from the first query, and it gave an error saying the database does not exist.
Or I could create an empty database to connect to and submit the queries to it, despite it not being used for anything.
最满意答案
您可以连接到postgres数据库,然后从那里运行drop database <DBNAME> ,是的。 另一种选择是,例如, template1 。 (我会避免使用template0,因为它本质上是创建template1的根模板,如果发生了某些事情,你总是可以从template0快速重新创建template1 ,假设你没有修改template1而不是template0 。)
我经常连接到postgres ,我自己,用于服务器级命令。
我跑了DROP DATABASE droptest; 在创建一个空数据库并看到它从pg_database查询返回后通过psql ,所以一般来说肯定是有效的。
也许它在某种程度上通过其他过程在你查询事物和你完成DROP...之间的过程中被删除了。
另一种选择是改为命令行工具dropdb 。 这是drop database的包装器,也是我通常用于手动和自动数据库丢弃实例的东西。
You could connect to the postgres database and then run drop database <DBNAME> from there, yes. Another option would be, say, template1. (I would avoid template0 since that's essentially the root template from which template1 was created, and you could always recreate template1 quickly from template0 if something happened to it, assuming you haven't modified template1 but not template0.)
I usually connect to postgres, myself, for server-level commands.
I ran DROP DATABASE droptest; via psql after creating an empty database and seeing it returned from a pg_database query, so that definitely works in general.
Perhaps it was somehow deleted via some other process in the interim between when you queried things and when you did the DROP....
Another option would be to shell out to the command line tool dropdb instead. This is a wrapper around drop database and is what I generally use both for manual and automated instances of database drops.
更多推荐
发布评论