我有一堆SQL脚本在数据库中创建表.每个表都位于一个单独的文件中,因此编辑它们要容易得多.
我想准备一个SQL脚本,它将创建完整的模式,创建表,插入测试数据并为表生成序列.
我能够为oracle数据库做这样的事情,但我遇到了postgres的问题.问题是 - 我不知道如何从另一个脚本运行表创建脚本.
在oracle中,我使用以下语法执行此操作:
@@'path of the script related to the path of the currently running sql file'
Run Code Online (Sandbox Code Playgroud)
一切都像一个魅力.
在postgres我试图寻找类似的东西,发现这个:
\ir 'relative path to the file'
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我运行我的主脚本时,我收到消息:
No such file or directory.
Run Code Online (Sandbox Code Playgroud)
示例调用在这里:
\ir './tables/map_user_groups.sql'
Run Code Online (Sandbox Code Playgroud)
我使用Postgres 9.3.我试图使用psql运行脚本:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
Run Code Online (Sandbox Code Playgroud)
除了调用其他脚本外,该文件执行正常.
有人知道如何解决这个问题吗?
如果问题中的某些内容不清楚 - 请告诉我:)
use*_*ser 10
基于答案可以从SQL脚本引用另一个SQL文件,在PostgreSQL上,您可以使用\i
语法包含另一个SQL文件.我刚刚测试过并且在PostgreSQL 9.6上运行良好:
\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;
Run Code Online (Sandbox Code Playgroud)
通过@wildplasser评论:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
Run Code Online (Sandbox Code Playgroud)
从psql的角度来看,main_sql
文件只是stdin,而stdin没有"filename".使用-f filename
提交一份文件,一个名字:
psql -U postgres -h localhost -d postgres -f filename.sql
Run Code Online (Sandbox Code Playgroud)
如何从另一个脚本运行postgres sql脚本?
似乎问题如下:如何从PostgreSQL中的sql脚本导入外部sql脚本?
归档时间: |
|
查看次数: |
9337 次 |
最近记录: |