Postgresql 如何从另一个脚本运行postgres sql脚本?

Postgresql 如何从另一个脚本运行postgres sql脚本?,postgresql,Postgresql,我有一堆SQL脚本在数据库中创建表。每个表都位于一个单独的文件中,因此编辑它们要容易得多 我想准备一个SQL脚本,它将创建完整的模式、创建表、插入测试数据并为表生成序列 我能够为oracle数据库做这样的事情,但我在postgres上遇到了问题。 问题是-我不知道如何从另一个脚本运行表创建脚本 在oracle中,我使用以下语法执行此操作: @@'path of the script related to the path of the currently running sql file' 一

我有一堆SQL脚本在数据库中创建表。每个表都位于一个单独的文件中,因此编辑它们要容易得多

我想准备一个SQL脚本,它将创建完整的模式、创建表、插入测试数据并为表生成序列

我能够为oracle数据库做这样的事情,但我在postgres上遇到了问题。 问题是-我不知道如何从另一个脚本运行表创建脚本

在oracle中,我使用以下语法执行此操作:

@@'path of the script related to the path of the currently running sql file'
一切都像一个符咒

在博士后,我试图寻找一些相似的东西,发现:

\ir 'relative path to the file'
不幸的是,当我运行主脚本时,我收到了以下消息:

No such file or directory.
示例调用如下所示:

\ir './tables/map_user_groups.sql'
我使用Postgres 9.3。我尝试使用psql运行脚本:

psql -U postgres -h localhost -d postgres < "path to my main sql file"

psql-U postgres-h localhost-d postgres<“我的主sql文件的路径”
除了调用其他脚本外,该文件执行良好

有人知道如何解决这个问题吗

如果问题中有不清楚的地方,请告诉我:)

根据答案,在PostgreSQL上,您可以使用
\i
语法包含另一个SQL文件。我刚刚在PostgreSQL 9.6上测试并运行良好:

\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;
通过@wildplasser评论:

如何从另一个脚本运行postgres sql脚本


似乎与以下问题相同:

您试图在哪个操作系统中完成此任务?
psql-U postgres-h localhost-d postgres
:从psql的角度来看,主sql文件只是stdin。stdin没有“文件名”;使用
-f filename
提交带有名称的文件。谢谢@wildplasser,就是这样:)我在Windows 7上完成了Professional@wildplasser我可以建议你把这个作为一个答案吗?问题是:对于unix用户来说,这个解决方案太琐碎了,以至于无法作为答案发布。。。
psql -U postgres -h localhost -d postgres < "path to my main sql file"
psql -U postgres -h localhost -d postgres -f filename.sql