postgreSQL - psql \i:如何在给定路径中执行脚本

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

postgreSQL - psql \i : how to execute script in a given path

postgresql

提问by dahpgjgamgan

I'm new to postgreSQL and I have a simple question:

我是 postgreSQL 的新手,我有一个简单的问题:

I'm trying to create a simple script that creates a DB so I can later call it like this:

我正在尝试创建一个简单的脚本来创建一个数据库,以便我以后可以这样调用它:

psql -f createDB.sql

I want the script to call other scripts (separate ones for creating tables, adding constraints, functions etc), like this:

我希望脚本调用其他脚本(用于创建表、添加约束、函数等的单独脚本),如下所示:

\i script1.sql
\i script2.sql

It works fine provided that createDB.sql is in the same dir.

只要 createDB.sql 在同一个目录中,它就可以正常工作。

But if I move script2 to a directory under the one with createDB, and modify the createDB so it looks like this:

但是,如果我将 script2 移动到带有 createDB 的目录下,并修改 createDB 使其看起来像这样:

\i script1.sql
\i somedir\script2.sql

I get an error:

我收到一个错误:

psql:createDB.sql:2: somedir: Permission denied

psql:createDB.sql:2: somedir: 权限被拒绝

I'm using Postgres Plus 8.3 for windows, default postgres user.

我在 Windows 上使用 Postgres Plus 8.3,默认 postgres 用户。

EDIT:

编辑:

Silly me, unix slashes solved the problem.

愚蠢的我,unix 斜线解决了这个问题。

回答by Steve K

Postgres started on Linux/Unix. I suspect that reversing the slash with fix it.

Postgres 开始于 Linux/Unix。我怀疑通过修复它来反转斜线。

\i somedir/script2.sql 

If you need to fully qualify something

如果你需要完全限定某些东西

\i c:/somedir/script2.sql

If that doesn't fix it, my next guess would be you need to escape the backslash.

如果这不能解决问题,我的下一个猜测是您需要转义反斜杠。

\i somedir\script2.sql

回答by Grant Johnson

Have you tried using Unix style slashes (/ instead of \)?

您是否尝试过使用 Unix 风格的斜杠(/ 而不是 \)?

\ is often an escape or command character, and may be the source of confusion. I have never had issues with this, but I also do not have Windows, so I cannot test it.

\ 通常是转义字符或命令字符,可能是混淆的根源。我从来没有遇到过这个问题,但我也没有 Windows,所以我无法测试它。

Additionally, the permissions may be based on the user running psql, or maybe the user executing the postmaster service, check that both have read to that file in that directory.

此外,权限可能基于运行 psql 的用户,或者可能是执行 postmaster 服务的用户,检查两者是否已读取该目录中的该文件。

回答by phipex

Try this, I work myself to do so

试试这个,我自己努力做到这一点

\i 'somedir\script2.sql'

回答by shiba sahu

i did try this and its working in windows machine to run a sql file on a specific schema.

我确实尝试过这个,它在 Windows 机器上工作以在特定模式上运行 sql 文件。

psql -h localhost -p 5432 -U username -d databasename -v schema=schemaname < e:\Table.sql

psql -h localhost -p 5432 -U 用户名 -d 数据库名 -v 模式=模式名 < e:\Table.sql