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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27941275/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:45:01  来源:igfitidea点击:

How to run postgres sql script from another script?

postgresql

提问by Marcin Roguski

I have a bunch of SQL scripts that create tables in the database. Each table is located in a separate file so that editing them is much easier.

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

I wanted to prepare a single SQL script that will create the full schema, create tables, insert test data and generate sequences for the tables.

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

I was able to do such thing for oracle database but I am having problems with postgres. The thing is - I do not know how to run the table creating script from another script.

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

In oracle I do it using the following syntax:

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

@@'path of the script related to the path of the currently running sql file'

And everything works like a charm.

一切都像魅力一样。

In postgres I was trying to search for something alike and found this:

在 postgres 中,我试图搜索类似的东西,发现了这个:

\ir 'relative path to the file'

Unfortunately when I run my main script I get the message:

不幸的是,当我运行主脚本时,我收到以下消息:

No such file or directory.

The example call is here:

示例调用在这里:

\ir './tables/map_user_groups.sql'

I use Postgres 9.3. I tried to run the script using psql:

我使用 Postgres 9.3。我尝试使用 psql 运行脚本:

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

The file executes fine except for the calling of those other scripts.

除了调用其他脚本外,该文件执行良好。

Does anybody know how to solve the problem ?

有人知道如何解决问题吗?

If something in the question is unclear - just let me know :)

如果问题中的某些内容不清楚-请告诉我:)

采纳答案by user

Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \isyntax. I just tested and is working good on PostgreSQL 9.6:

根据答案It is possible to reference another SQL file from SQL script,在 PostgreSQL 上,您可以仅使用\i语法包含另一个 SQL 的文件。我刚刚测试过并且在 PostgreSQL 9.6 上运行良好:

\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;

By the @wildplasser comment:

通过@wildplasser 评论:

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

From psql's perspective the main_sqlfile is just stdin, and stdin has no "filename". Use -f filenameto submit a file with a name:

从 psql 的角度来看,main_sql文件只是标准输入,标准输入没有“文件名”。使用-f filename提交一份文件,一个名字:

psql -U postgres -h localhost -d postgres -f filename.sql

How to run postgres sql script from another script?

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

Seems the same question as: How to import external sql scripts from a sql script in PostgreSQL?

似乎与以下问题相同:如何从 PostgreSQL 中的 sql 脚本导入外部 sql 脚本?