PostgreSQL - query from bash script as database user 'postgres'

后端 未结 8 1161
[愿得一人]
[愿得一人] 2020-12-22 19:22

I have a table in my PostgreSQL database which has 3 columns - c_uid, c_defaults and c_settings. c_uid simply stores the

8条回答
  •  春和景丽
    2020-12-22 19:50

    if you are planning to run it from a separate sql file. here is a good example (taken from a great page to learn how to bash with postgresql http://www.manniwood.com/postgresql_and_bash_stuff/index.html

    #!/bin/bash
    set -e
    set -u
    if [ $# != 2 ]; then
       echo "please enter a db host and a table suffix"
       exit 1
    fi
    
    export DBHOST=$1
    export TSUFF=$2
    psql \
      -X \
      -U user \
      -h $DBHOST \
      -f /path/to/sql/file.sql \
      --echo-all \
      --set AUTOCOMMIT=off \
      --set ON_ERROR_STOP=on \
      --set TSUFF=$TSUFF \
      --set QTSTUFF=\'$TSUFF\' \
       mydatabase
    
       psql_exit_status = $?
    
       if [ $psql_exit_status != 0 ]; then
         echo "psql failed while trying to run this sql script" 1>&2
         exit $psql_exit_status
       fi
    
       echo "sql script successful"
    exit 0
    

提交回复
热议问题