agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
insert a text file into a variable in order to insert into a bytea column
8+ messages / 3 participants
[nested] [flat]

* insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 18:10  Arthur Lewis <arthur.lewis@hypermediasystems.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Arthur Lewis @ 2016-01-28 18:10 UTC (permalink / raw)
  To: pgsql-sql

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.

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


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 18:20  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Arthur Lewis <arthur.lewis@hypermediasystems.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Adrian Klaver @ 2016-01-28 18:20 UTC (permalink / raw)
  To: Arthur Lewis <arthur.lewis@hypermediasystems.com>; pgsql-sql

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@killi:~> cat test.txt
Test
second line

aklaver@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.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 18:46  Arthur Lewis <arthur.lewis@hypermediasystems.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Arthur Lewis @ 2016-01-28 18:46 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: 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.klaver@aklaver.com>
To: "Arthur Lewis" <arthur.lewis@hypermediasystems.com>, pgsql-sql@postgresql.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@killi:~> cat test.txt
Test
second line

aklaver@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.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 20:08  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Arthur Lewis <arthur.lewis@hypermediasystems.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Adrian Klaver @ 2016-01-28 20:08 UTC (permalink / raw)
  To: Arthur Lewis <arthur.lewis@hypermediasystems.com>; +Cc: pgsql-sql

On 01/28/2016 10:46 AM, Arthur Lewis wrote:
> 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`.

Hmm, well there went that theory;)

What happens if you do?:

select octet_length(:'stext');

>
> Arthur Lewis
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 21:08  Arthur Lewis <arthur.lewis@hypermediasystems.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Arthur Lewis @ 2016-01-28 21:08 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-sql

postgres=> \set stext `cat file.txt`
postgres=> select octet_length(:'stext');
 octet_length 
--------------
        53603

in my shell I did the following:
$ ls -l file.txt
-rw-rw-r-- 1 alewis alewis 53604 Jan 27 08:30 file.txt

I'm still one byte short.  So the problem has to be either the \set command chomping it or cat.  I just tested cat and that seems to be working.


----- Original Message -----
From: "Adrian Klaver" <adrian.klaver@aklaver.com>
To: "Arthur Lewis" <arthur.lewis@hypermediasystems.com>
Cc: pgsql-sql@postgresql.org
Sent: Thursday, January 28, 2016 12:08:03 PM
Subject: Re: [SQL] insert a text file into a variable in order to insert into a bytea column

On 01/28/2016 10:46 AM, Arthur Lewis wrote:
> 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`.

Hmm, well there went that theory;)

What happens if you do?:

select octet_length(:'stext');

>
> Arthur Lewis
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 21:57  David G. Johnston <david.g.johnston@gmail.com>
  parent: Arthur Lewis <arthur.lewis@hypermediasystems.com>
  0 siblings, 1 reply; 8+ messages in thread

From: David G. Johnston @ 2016-01-28 21:57 UTC (permalink / raw)
  To: Arthur Lewis <arthur.lewis@hypermediasystems.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-sql

On Thu, Jan 28, 2016 at 2:08 PM, Arthur Lewis <
arthur.lewis@hypermediasystems.com> wrote:

> postgres=> \set stext `cat file.txt`
> postgres=> select octet_length(:'stext');
>  octet_length
> --------------
>         53603
>
> in my shell I did the following:
> $ ls -l file.txt
> -rw-rw-r-- 1 alewis alewis 53604 Jan 27 08:30 file.txt
>
> I'm still one byte short.  So the problem has to be either the \set
> command chomping it or cat.  I just tested cat and that seems to be working.
>
>
​Or the "back-ticks" implementation...

David J.​

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 22:36  Arthur Lewis <arthur.lewis@hypermediasystems.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Arthur Lewis @ 2016-01-28 22:36 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-sql

Hi David,
Yes you're correct.  The problem is caused by the back-ticks (`)  Here is what I found in the docs about back-ticks

"Within an argument, text that is enclosed in backquotes (`) is taken as a command line that is passed to the shell. The output of the command (with any trailing newline removed) replaces the backquoted text."

Arthur Lewis



----- Original Message -----
From: "David G. Johnston" <david.g.johnston@gmail.com>
To: "Arthur Lewis" <arthur.lewis@hypermediasystems.com>
Cc: "Adrian Klaver" <adrian.klaver@aklaver.com>, pgsql-sql@postgresql.org
Sent: Thursday, January 28, 2016 1:57:38 PM
Subject: Re: [SQL] insert a text file into a variable in order to insert into a bytea column



On Thu, Jan 28, 2016 at 2:08 PM, Arthur Lewis < arthur.lewis@hypermediasystems.com > wrote: 




postgres=> \set stext `cat file.txt` 
postgres=> select octet_length(:'stext'); 
octet_length 
-------------- 
53603 

in my shell I did the following: 
$ ls -l file.txt 
-rw-rw-r-- 1 alewis alewis 53604 Jan 27 08:30 file.txt 

I'm still one byte short. So the problem has to be either the \set command chomping it or cat. I just tested cat and that seems to be working. 







​Or the "back-ticks" implementation... 




David J.​


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: insert a text file into a variable in order to insert into a bytea column
@ 2016-01-28 23:01  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Arthur Lewis <arthur.lewis@hypermediasystems.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Adrian Klaver @ 2016-01-28 23:01 UTC (permalink / raw)
  To: Arthur Lewis <arthur.lewis@hypermediasystems.com>; David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-sql

On 01/28/2016 02:36 PM, Arthur Lewis wrote:
> Hi David,
> Yes you're correct.  The problem is caused by the back-ticks (`)  Here is what I found in the docs about back-ticks
>
> "Within an argument, text that is enclosed in backquotes (`) is taken as a command line that is passed to the shell. The output of the command (with any trailing newline removed) replaces the backquoted text."

Huh, I missed that in reading the docs.

So:

test-# \set stext `cat test.txt`
test-# \echo :stext 

Test 
 

second line 
 

test-# \set stext `cat test.txt` '\n' 

test-# \echo :stext 

Test 
 

second line 
 

 
 

test-#

New lesson learned.

>
> Arthur Lewis
>



-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2016-01-28 23:01 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-28 18:10 insert a text file into a variable in order to insert into a bytea column Arthur Lewis <arthur.lewis@hypermediasystems.com>
2016-01-28 18:20 ` Adrian Klaver <adrian.klaver@aklaver.com>
2016-01-28 18:46   ` Arthur Lewis <arthur.lewis@hypermediasystems.com>
2016-01-28 20:08     ` Adrian Klaver <adrian.klaver@aklaver.com>
2016-01-28 21:08       ` Arthur Lewis <arthur.lewis@hypermediasystems.com>
2016-01-28 21:57         ` David G. Johnston <david.g.johnston@gmail.com>
2016-01-28 22:36           ` Arthur Lewis <arthur.lewis@hypermediasystems.com>
2016-01-28 23:01             ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox