如何在 postgreSQL 命令中指示在哪个数据库中执行脚本?(类似于 SQL Server 的“使用”命令)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3909123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to indicate in postgreSQL command in which database to execute a script? (simmilar to SQL Server "use" command)
提问by paddingtonMike
I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
我有以下问题,我需要放入一个脚本,该脚本将在新版本推出之前运行在 PostgreSQL 中启用 pgAgent 的 SQL 代码。但是,此代码应在维护数据库 (postgres) 上运行,而我们运行脚本文件的数据库是另一个数据库。
I remember that in SQL Server there is a command "use " so you could do something like:
我记得在 SQL Server 中有一个命令“use”,因此您可以执行以下操作:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
PostgreSQL中有类似的东西吗?
采纳答案by paddingtonMike
well after looking on the web for some time I found this which was what I need it http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
在网上看了一段时间后,我发现这是我需要的 http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-服务器.html
回答by panos
You can put in your file something like:
您可以在文件中放入以下内容:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
回答by Scott Bailey
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
你不能用这种方式在 Postgres 中切换数据库。您实际上必须重新连接到另一个数据库。
回答by Neall
Are you piping these commands through the psql
command? If so, \c databasename
is what you want.
您是否通过psql
命令管道这些命令?如果是这样,\c databasename
就是你想要的。
回答by Kuberchaun
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
PostgreSQL 没有 USE 命令。您很可能会使用带有 --dbname 选项的 psql 来完成此操作, --dbname 将数据库名称作为参数。有关您可以传入的其他选项的详细信息,请参阅此链接,您还需要查看 --file 选项。 http://www.postgresql.org/docs/9.0/interactive/app-psql.html