views:

51

answers:

3

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]

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 dont 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 pgl? Thanks

A: 

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

Frank Heikens
ill be importing a whole lot of text and using cat is not idea. Thanks
monmonja
A: 

Sorry, my answer was not quite what you were asking :(

Edwin Knuth
A: 

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.

John P
yap saw this but i need to use expect in bash so that i could use the import statement, i was looking for something like input pipe in mysqlmysql dbname -uuser -pmypwd < /tmp/queries.sqlThanks
monmonja
Then perhaps copy might help you.Citing the PostgreSQL documentation: COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already).
John P