≡ Menu

How to Execute PostgreSQL Commands Inside Unix Shell Scripts

Question: How do I executed PostgreSQL Commands inside a Linux / UNIX shell script?

Answer: With the help of the psql interactive terminal, you can execute the psql commands from the shell script. For this purpose, you should enable the password less login by pg_hba.conf, or .pgpass.

Syntax

psql DBNAME USERNAME << EOF
     statement 1;
     statement 2;
     .
     .
     statement n;
EOF

PostgreSQL: Executing SQL from shell scripts

Provide all the postgreSQL commands between the EOF block as shown below.

#!/bin/sh

dbname="test"
username="test"
psql $dbname $username << EOF
SELECT * FROM test;
EOF

PostgreSQL: Using variables in SQL from shell scripts

You can also use shell script variable inside the EOF block as shown below.

#!/bin/sh

dbname="test"
username="test"
wherecond="tgs"
psql $dbname $username << EOF
SELECT * FROM test WHERE col_name = '$wherecond';
EOF
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • r March 26, 2010, 4:26 am

    What about PGPASSWORD ?

  • Hariharasubramanian March 26, 2010, 6:00 am

    One more way:

    psql -U -c “select * from test”;

  • Rad Cirskis May 6, 2012, 6:00 pm

    You can take advantage of environment variables used by
    http://www.postgresql.org/docs/9.1/static/libpq-envars.html , eg, PGDATABASE , PGUSER, PGPASSWORD etc.

    Then you don’t need to provide psql parameters. It is useful if you have to run psql multiple times:

    export PGDATABASE=test
    export PGUSER=test
    wherecond=”tgs”
    psql << EOF
    SELECT * FROM test WHERE col_name = '$wherecond';
    EOF

  • subos September 12, 2012, 7:29 am

    That way the queue is printed in terminal window. How can it be “exported” to CSV (for example) ?

  • João Paulo June 6, 2013, 9:32 am

    psql << EOF
    \copy (select * from table) to '/tmp/file.csv';
    EOF

  • zero May 19, 2015, 3:42 pm

    i am getting errors when using a bash variable inside the EOF any idea what might be wrong?

    psql $db << EOF
    \copy '$tbl' FROM '/Users/some/file/path.csv' DELIMITER ',' CSV HEADER;
    EOF

  • Tanay May 31, 2016, 6:35 am

    I am trying the following way, but its not working… any suggestion please ?

    LD_LIBRARY_PATH=/opt/app/apphome/AAPWebInfrastructurePlatformDataServer/9.4/lib
    export LD_LIBRARY_PATH
    PGPASSWORD=ZZZ
    export PGPASSWORD
    /opt/app/apphome/AAPWebInfrastructurePlatformDataServer/9.4/bin/psql -U USR -h localhost -p 9432 -d XXX

    <<EOF
    select * from baseview where object_type_id=106 and user_id=’USR’;
    EOF