Advertisement
sokomo

psql_grant_table_and_database

Apr 7th, 2022
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.60 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e
  3.  
  4. _DB_HOST='127.0.0.1'
  5. _DB_PORT='5432'
  6. _DB_USER='postgres'
  7. _DB_PASSWD='password'
  8.  
  9. _PSQL_CLI='psql'
  10.  
  11. _DB_LIST=$(cat prod_db_list.txt)
  12. _PSQL_COMMON="${_PSQL_CLI} -h ${_DB_HOST} -p ${_DB_PORT} -U ${_DB_USER}"
  13.  
  14. export PGPASSWORD="${_DB_PASSWD}"
  15.  
  16. for _db_name in ${_DB_LIST[@]};
  17. do
  18.   _TABLE_NAMES=$(${_PSQL_COMMON} -d ${_db_name} -t -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';")
  19.   for _table_name in ${_TABLE_NAMES[@]};
  20.   do
  21.     echo ${_PSQL_COMMON} -d ${_db_name} -c "grant all on public.${_table_name} to pguser;"
  22.   done
  23. done
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement