Skip to content

Instantly share code, notes, and snippets.

@oztalha
Created September 3, 2014 21:42
Show Gist options
  • Save oztalha/0300f8a4f6f64f22c4ee to your computer and use it in GitHub Desktop.
Save oztalha/0300f8a4f6f64f22c4ee to your computer and use it in GitHub Desktop.
Grant SELECT access to a user for all tables in a PostgreSql DB
#!/bin/bash
echo '---------------------------------------------------------------'
echo 'Grant SELECT access to a user for all tables in a PostgreSql DB'
echo '[USAGE]: ./scriptname username dbname'
echo '---------------------------------------------------------------'
for table in `echo "SELECT schemaname || '.' || relname FROM pg_stat_all_tables;" | psql $2 | grep -v "pg_" | grep "^ " | grep -v "\?column\?"`;
do
echo "GRANT SELECT ON TABLE $table TO $1;"
echo "GRANT SELECT ON TABLE $table TO $1;" | psql $2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment