Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop PostgreSQL from spliting value(s) in multiple lines?

I'm converting a binary data (bytea) data type to string using encode(foo::bytea, 'base64') but the output is being split in multiple lines:

-[ RECORD 1 ]-+-----------------------------------------------------------------------
 req_id       | 132675
 b_string     | d4IF4jCCBd4GCSqGSIb3DQEHAqCCBc8wggXLAgEDMQ0wCwYJBAIBMIGYBgZngQgBAQGg+
              | gY0EgYowgYcCAQAwCwYJYIZIAQAwCwYJYIZIAWUDBAIHUwUdH0JybzpY2evf+v9Xg86b+
              | HSGTGYBIb/QwJQIBAgQg1M6/cJ+S39XY1lm43oenxJNLrYcc3hVw7fgwJQIBDgQgIAil+
              | 1JnYbdS0p4pK07kMkb/dbMcxryx6mqbLTzx+YJ6gggQbMI2gAwIBAgIESS7vwTAKBggq+
              | LUxRjUXbTgfGwUKOFwemsc4KXbsLZ13MkbNfAQ==

How can get a single string instead?

UPDATE: based on the solution by @LaurenzAlbe

Just for the completeness, this is what I end up doing the gave me the result I wanted:

translate(encode(foo::bytea, 'base64'), E'\n', '')
like image 898
MacUsers Avatar asked Dec 10 '18 17:12

MacUsers


1 Answers

psql doesn't split your string in multiple lines.

It's the string that contains new-line characters (ASCII 10), and psql displays them accurately. The + at the end of every line is psql's way of telling you that the value is continued in the next line.

You can use the unaligned mode (psql option -A) to get rid of the +, but the output format is less attractive then.

You can get rid of the newlines in a string with

SELECT translate(..., E'\n', '');

decode will be able to handle such a string.

like image 162
Laurenz Albe Avatar answered Nov 04 '22 01:11

Laurenz Albe