Re: insert a text file into a variable in order to insert into a bytea column

From: Arthur Lewis <arthur(dot)lewis(at)hypermediasystems(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: insert a text file into a variable in order to insert into a bytea column
Date: 2016-01-28 18:46:16
Message-ID: 877971630.7184594.1454006776740.JavaMail.root@hypermediasystems.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,
In my shell I did the following

$ cat file > file.temp
$ ls -l file file.temp
-rw-rw-r-- 1 alewis alewis 53604 Jan 27 08:30 file
-rw-rw-r-- 1 alewis alewis 53604 Jan 28 10:28 file.temp
$ diff file file.temp
$

So at least in the shell the cat command isn't losing a byte at the end of the file like in psql \set x `cat file`.

Arthur Lewis

----- Original Message -----
From: "Adrian Klaver" <adrian(dot)klaver(at)aklaver(dot)com>
To: "Arthur Lewis" <arthur(dot)lewis(at)hypermediasystems(dot)com>, pgsql-sql(at)postgresql(dot)org
Sent: Thursday, January 28, 2016 10:20:43 AM
Subject: Re: [SQL] insert a text file into a variable in order to insert into a bytea column

On 01/28/2016 10:10 AM, Arthur Lewis wrote:
> Hello,
> I have a file called file.txt that is 53604 bytes. When I do the following
>
> \set stext `cat file.txt`
>
> select length(:'stext'); length
> --------
> 53603
>
> it seems that one byte (the last newline character) is chomped off. How can I import a text file into a variable and insert it into a bytea column without losing the last newline byte.

Are sure it is not cat that is chomping the byte?:

aklaver(at)killi:~> cat test.txt
Test
second line

aklaver(at)killi:~> cat -A test.txt
Test$
second line$

>
> What I want to do is something like this:
>
> \set stext `cat file.txt`
> insert into stored_files (file_name,file_text) values ('file',:'stext');
>
> without losing a character within the variable stext.
>
> In table stored_files, file_name is text and file_text is bytea.
>
> I'm using psql 9.3.10 on Ubuntu 14 Linux (32 bit.)
> I cannot use pg_read_file because that is for the server side, and I'm on the client side.
>
> There should be an easy way to get a text file into a database column.
>
> Thank you,
> Arthur Lewis
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2016-01-28 20:08:03 Re: insert a text file into a variable in order to insert into a bytea column
Previous Message Adrian Klaver 2016-01-28 18:20:43 Re: insert a text file into a variable in order to insert into a bytea column