Reputation: 41
When I execute my query in PostgreSQL:
SELECT names
from user;
I obtain the following result:
names
--------------
Anna
Julius
Perico
(3 rows)
What I want to get is the following output:
Anna Julius Perico
I need this because is a bash script ant I need to save it in a variable.
Upvotes: 4
Views: 1578
Reputation: 246268
If you want to use it in a shell script, this would be the best way:
myvar=`psql --no-align --quiet --tuples-only --command='SELECT name FROM users'`
No need to have them in one line; the shell accepts a line feed as field separator as well:
for n in $myvar; do
echo "name: $n"
done
name: Anna
name: Julius
name: Perico
Upvotes: 3
Reputation: 520968
The string_agg
function might be what you want here:
select string_agg(names, ' ' order by names) from user;
I said "might" because this assumes that the names
column itself could be used to generate the order in the single string output. If you want a different order, then you would need another column.
Upvotes: 1