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 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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 mysql mysql dbname -uuser -pmypwd < /tmp/queries.sql Thanks
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).
1

In case of more detailed parameters:

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

Comments

0

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

1 Comment

ill be importing a whole lot of text and using cat is not idea. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.