Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How can I concatenate two psql (PostgreSQL client) variables? I want to generate an absolute path by concatenating a directory path variable and a filename variable.

I've tried this:

\set path '/tmp/'
\set file 'foo'
\echo :path:file

But psql puts a space between the path and the file, and outputs:

/tmp/ foo
share|improve this question
add comment

3 Answers

up vote 5 down vote accepted
\set path '/tmp/'
\set file 'foo'
\set pf :path:file \echo :pf
/tmp/foo

Why does this work? I quote the manual here:

\set [ name [ value [ ... ] ] ]

Sets the internal variable name to value or, if more than one value is given, to the concatenation of all of them. [...]

Emphasis mine.

share|improve this answer
    
Even better than mine –  dezso Jul 19 '12 at 6:23
    
That's perfect, thanks –  Daniel Serodio Jul 19 '12 at 13:21
add comment

Try this:

\set path /tmp/
\set file foo
\qecho :path:file \o | sed s/\ //
/tmp/foo

\qecho writes to the query output channel (unlike \echo, which writes to standard output). \o | then redirects the output to the subsequent command.

share|improve this answer
add comment

It looks like you want either the || operator or the concat function:

http://www.postgresql.org/docs/9.1/static/functions-string.html

share|improve this answer
1  
How do you use these with \echo? –  dezso Jul 18 '12 at 12:26
    
This answer refers to concatenation in SQL while the question is about psql (the terminal interface). –  Erwin Brandstetter Feb 4 '13 at 20:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.