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. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 down vote accepted
\set path '/tmp/'
\set file 'foo'
\set pf :path:file \echo :pf

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

Try this:

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

\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

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

share|improve this answer
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

Your Answer


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.