Postgresql: inserting value of a column from a file

file-io, postgresql

Solution

If this SQL code is executed dynamically from your programming language, use the means of that language to read the file, and execute a plain INSERT statement.

However, if this SQL code is meant to be executed via the `psql` command line tool, you can use the following construct:

\set content `cat file`
INSERT INTO testtable VALUES(15, :'content');

Note that this syntax is specific to `psql` and makes use of the `cat` shell command.

It is explained in detail in the PostgreSQL manual:

- psql / SQL Interpolation

- psql / Meta-Commands

Problem

For example, there is a table named 'testtable' that has following columns: testint (integer) and testtext (varchar(30)). What i want to do is pretty much something like that: ``` INSERT INTO testtable VALUES(15, CONTENT_OF_FILE('file')); ``` While reading postgresql documentation, all I could find is COPY TO/FROM command, but that one's applied to tables, not single columns. So, what shall I do?

Original source