Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store PostgreSQL query result to Shell or PostgreSQL variable

For instance, I have a table stores value:

select * from myvalue;

  val
-------
 12345
(1 row)

How can I save this 12345 into a variable in postgresql or shell script?

Here's what I tried in my shell script:

var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)

but echo $var gives me:

val ------- 12345 (1 row)

I've also tried

\set var (select * from myvalue)

in psql and when I type \set it lists:

var = '(select*frommyvalue)'
like image 763
Rock Avatar asked Sep 21 '12 18:09

Rock


People also ask

How do you assign a selected value to a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.

Can you use variables in PostgreSQL?

PL/pgSQL variables can have any SQL data type, such as integer , varchar , and char . Here are some examples of variable declarations: user_id integer; quantity numeric(5); url varchar; myrow tablename%ROWTYPE; myfield tablename.

How do I get into PostgreSQL shell?

Getting a PostgreSQL command prompt You can get a command shell in Windows by running cmd.exe. The CSEP544 shell launcher script will also open a shell for you. Type psql -U postgres at the prompt, and hit Enter. Here, postgres represents the username of the database superuser.


5 Answers

No, no, no! Use "raw data" switch from psql, like "-t" or "\t" and pipe the query to psql instead of parsing ascii-table, come on :-)

echo 'select * from myvalue;' | psql -t -h host -U user -d db

If you really need parse psql output, you could also use -H switch ( turns on HTML output ), and parse it with some perl module for parsing html tables, I used that once or twice.. Also, you may want to use a pgpass file and ~/.psqlrc for some defaults, like default DB to connect, when not specified.

like image 155
Piotr Wadas Avatar answered Oct 12 '22 11:10

Piotr Wadas


psql has a -c/--command= option to accept SQL from the command line, and -t/--tuples-only option to control output formatting.

$ psql -c 'select 1+1'
 ?column? 
----------
        2
(1 row)
$ psql -t -c 'select 1+1'
        2

$ VALUE=`psql -t -c 'select 1+1'`
$ echo $VALUE
2
like image 34
willglynn Avatar answered Oct 12 '22 09:10

willglynn


var=`psql -Atc "select 1;"`
echo $var
1
like image 44
Scott Marlowe Avatar answered Oct 12 '22 09:10

Scott Marlowe


In this answer I explain one way to do it, using a co-process to communicate back-and-forth with psql. That's overkill if all you need is to run a query and get a single result, but might be good to know if you're shell scripting with psql.

like image 43
Craig Ringer Avatar answered Oct 12 '22 10:10

Craig Ringer


You can filter the result you get with your psql command:

var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
var=$(cut -d' ' -f3 <<<$var)
like image 35
Stephane Rouberol Avatar answered Oct 12 '22 11:10

Stephane Rouberol