Bash script & PostgreSQL: How to access column values returned from SELECT statement
bash, postgresql, sql
Solution
You can store your results into an array and loop through it using a while loop.
psql -d customers -c "select * from CustomerInfo where name = 'Dave'"
| while read -a Record ; do
# ${Record[0]} is your ID field
# ${Record[1]} is your Name field
# ${Record[2]} is your address field
done
Problem
I have a database in PostgreSQL called customers, customers has a table called CustomerInfo. CustomerInfo contains 3 columns ID, Name and address. I would like to write a bash script to get the information from the CustomerInfo table but i am not sure how to access the individual rows once i have the results of the query. Here is the script i have written: ``` #!/bin/bash results=`psql -d customers -c "select * from CustomerInfo where name = 'Dave'"` echo $results['name'] ``` The query runs correctly and returns the correct results but the echo command will just print everything in results. I know this is not the correct way of doing this, does anyone know of a way to get the query results as an array, or would i just have to write my own function for parsing the results? Thanks!