如何在 postgreSQL 命令中指示在哪个数据库中执行脚本?(类似于 SQL Server 的“使用”命令)

pad*_*ike 13 sql postgresql

我有以下问题,我需要放入一个脚本,该脚本将在新版本推出之前运行在 PostgreSQL 中启用 pgAgent 的 SQL 代码。但是,此代码应在维护数据库 (postgres) 上运行,而我们运行脚本文件的数据库是另一个数据库。

我记得在 SQL Server 中有一个命令“use”,因此您可以执行以下操作:

use foo

-- some code

use bar 

-- more code
Run Code Online (Sandbox Code Playgroud)

PostgreSQL中有类似的东西吗?

小智 9

您可以在文件中放入以下内容:

\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Run Code Online (Sandbox Code Playgroud)