开发者

PostgreSQL import file

开发者 https://www.devze.com 2023-01-05 01:07 出处:网络
In PostgreSQL and bash(linux), are there ways to directly import a file from the filesystem like [execute.sh]

In PostgreSQL and bash(linux), are there ways to directly import a file from the filesystem like

[execute.sh]

pgsql .... -f insert.txt

[insert.txt]

开发者_JAVA技巧insert into table(id,file) values(1,import('/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)

And can how do I move the insert.txt statement to PostgreSQL?


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:

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.

Hope this was what you asked for.


In case of more detailed parameters:

$ psql -h [host] -p [port] -d [databaseName] -U [user] -f [/absolute/path/to/file]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号