bash PostgreSQL 导入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3107018/
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
PostgreSQL import file
提问by monmonja
In PostgreSQL and bash(linux), are there ways to directly import a file from the filesystem like
在 PostgreSQL 和 bash(linux) 中,有没有办法直接从文件系统导入文件,比如
[execute.sh]
[执行.sh]
pgsql .... -f insert.txt
pgsql .... -f insert.txt
[insert.txt]
[插入.txt]
insert into table(id,file) values(1,import('/path/to/file'))
插入表(id,文件)值(1,导入('/path/to/file'))
There seems to be no import function, bytea_import as well, lo_import would save int and I don't know how to get the file back (these files are in small sizes so using lo_import seems not appropriate)
似乎没有导入功能,bytea_import 也是如此,lo_import 会保存 int 而我不知道如何取回文件(这些文件很小,因此使用 lo_import 似乎不合适)
And can how do I move the insert.txtstatement to PostgreSQL?
以及如何将insert.txt语句移动到 PostgreSQL?
采纳答案by Frank Heikens
The manual has some examples:
手册中有一些例子:
testdb=> \set content '''' `cat my_file.txt` ''''
testdb=> INSERT INTO my_table VALUES (:content);
See http://www.postgresql.org/docs/8.4/interactive/app-psql.html
见http://www.postgresql.org/docs/8.4/interactive/app-psql.html
回答by John P
I'm not sure what you're after, but if you have script with SQL-statements, for example the insert statements that you mention, you can run psql and then run the script from within psql. For example:
我不确定您在追求什么,但是如果您有带有 SQL 语句的脚本,例如您提到的插入语句,您可以运行 psql,然后从 psql 中运行该脚本。例如:
postgres@server:~$ psql dbname
psql (8.4.1)
Type "help" for help.
dbname=# \i /tmp/queries.sql
This will run the statements in /tmp/queries.sql.
这将运行 /tmp/queries.sql 中的语句。
Hope this was what you asked for.
希望这是你所要求的。
回答by Tomá? Votruba
In case of more detailed parameters:
如果有更详细的参数:
$ psql -h [host] -p [port] -d [databaseName] -U [user] -f [/absolute/path/to/file]

