반응형
/********************************************************************************************
-- Title : [9.2] How to make a bash shell script for postgresql
-- Reference : dbrang.tistory.com
-- Key word : postgresql bash shell script 쉘 스크립트
********************************************************************************************/

-- when user is a "postgres "

#!/bin/bash


PGUSER=postgres

export PGUSER


# execute batch command.

psql $PGUSER << EOF

    select datname, datdba, encoding, datctype from pg_database;

    select current_timestamp;

EOF


# execute inline command.

psql -c "select '$PGUSER;' as USER "


# execute file command.

FILE=/home/postgres/file

export FILE


psql postgres -f "$FILE"                 ## There is a command as below

                                                  ##     "select datname from pg_database;"
                                                  ## in /home/postgres/file.

 
-- when user is a "root"

#!/bin/bash

PGHOME=/home/postgres/pgsql

PGCTL=$PGHOME/bin/pg_ctl


export PGHOME PGCTL


# execute postgresql command.

su - postgres -c "$PGCTL status"


# execute psql command as root.
su - postgres -c "psql -c \"select datname from pg_database;"\  "

# execute psql command with os command.

su - postgres -c "psql -c \"select '`date`' as today \"  " 

# execute file command.

FILE=/home/postgres/file

export FILE


su - postgres -c "psql -f \"$FILE\"  "
                                                  ## There is a command as below

                                                  ##     "select datname from pg_database;" 

                                                  ## in /home/postgres/file. 

 
반응형

+ Recent posts