Because I need to log into many servers, I created functions as keyboard shortcuts (not aliases, since I will want to embed these shortcuts in other functions).
psqlxyz () { echo "P1=$1"; echo "P2=$2"; psql -U postgres -h XYZ $@ } This is the (simple, test) command that I want to run, which works when run explicitly using psql, but not my function. Any ideas why the function isn't properly passing the "-c" and '"select ..."' to psql?
while getopts "d:p:uU:" OPT; do case "${OPT}" in d) DBNAME=$OPTARG ;; p) PORT="-p $OPTARG" ;; U) USER="-U $OPTARG" ;; u) usage ;; [?]) usage esac; done
if [ "$DBNAME" = "" ] then usage exit 1 fi
psql $PORT $USER postgres <<_EOF_ SELECT txid_current() AS txid_current; SELECT datname, rolname as owner, pg_size_pretty(pg_database_size(datname) )as size_pretty, pg_database_size(datname) as size, (SELECT pg_size_pretty (SUM( pg_database_size(datname))::bigint) FROM pg_database) AS total, (pg_database_size(datname) / (SELECT SUM( pg_database_size(datname)) FROM pg_database) ) * 100::numeric(6,3) AS pct FROM pg_database d JOIN pg_authid a ON a.oid = datdba WHERE datname LIKE '%$DBNAME%' ORDER BY datname; _EOF_