agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Arthur Lewis <arthur.lewis@hypermediasystems.com>
To: pgsql-sql@postgresql.org
Subject: Re: insert a text file into a variable in order to insert into a bytea column
Date: Thu, 28 Jan 2016 10:20:43 -0800
Message-ID: <56AA5BFB.30808@aklaver.com> (raw)
In-Reply-To: <1668948418.7183478.1454004653187.JavaMail.root@hypermediasystems.com>
References: <1668948418.7183478.1454004653187.JavaMail.root@hypermediasystems.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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



view thread (8+ messages)  latest in thread

Message-ID: <56AA5BFB.30808@aklaver.com>
Permalink:  ../56AA5BFB.30808@aklaver.com/
Also on:    postgresql.org/message-id/56AA5BFB.30808@aklaver.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: adrian.klaver@aklaver.com, arthur.lewis@hypermediasystems.com
  Subject: Re: insert a text file into a variable in order to insert into a bytea column
  In-Reply-To: <56AA5BFB.30808@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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