Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining multi-line strings in psql

I would like to paste

  123
  456
  789

into psql and to store multi-line string in some variable (ie. :str) for later use.

Is that possible? Now I'm getting unterminated quoted string error.

like image 564
mpapec Avatar asked Jul 16 '15 12:07

mpapec


People also ask

How do you define multiline strings?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How do I comment multiple lines in PostgreSQL?

Syntax Using /* and */ symbols In PostgreSQL, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.

Can double quote strings span multiple lines?

To create strings that span multiple lines, triple single quotes ''' or triple double quotes """ are used to enclose the string. ''' This string is on multiple lines within three single quotes on either side.


1 Answers

A little clunky, but in version 9.3 and up, you can do it with \gset in conjunction with a dollar-quoted literal:

SELECT
$$123
456
789$$ AS str \gset
like image 65
Nick Barnes Avatar answered Oct 03 '22 10:10

Nick Barnes