createuser creates db and sets perms in psql
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/3d432154 Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/3d432154 Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/3d432154 Branch: refs/heads/master Commit: 3d432154dd1c47df9ca82e8555f28dc9b61b5c26 Parents: 03f565f Author: Dan Kirkwood <dang...@gmail.com> Authored: Mon Jan 16 16:04:28 2017 -0700 Committer: Dan Kirkwood <dang...@gmail.com> Committed: Wed Jan 18 15:34:57 2017 -0700 ---------------------------------------------------------------------- traffic_ops/app/db/admin.pl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3d432154/traffic_ops/app/db/admin.pl ---------------------------------------------------------------------- diff --git a/traffic_ops/app/db/admin.pl b/traffic_ops/app/db/admin.pl index 324044e..dd19b0f 100755 --- a/traffic_ops/app/db/admin.pl +++ b/traffic_ops/app/db/admin.pl @@ -180,11 +180,17 @@ sub createdb { } sub createuser { - system("createuser -h $host_ip -p $host_port -P -e --superuser $db_username;"); + my $user_exists = system("psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='USR_NAME'" | grep -q 1 || createuser ... + + system("createuser -U traffic_ops -h $host_ip -p $host_port -P -e --superuser $db_username;"); + system("createdb -U traffic_ops -h $host_ip -p $host_port -U $db_username -e $db_name;"); + system("psql -U traffic_ops -h $host_ip -p $host_port -e -c 'GRANT ALL PRIVILEGES ON DATABASE $db_name TO $db_username;'"); + system("psql $db_name -U traffic_ops -h $host_ip -p $host_port -e -c 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $db_username;'"); + } sub dropuser { - system("dropuser -h $host_ip -p $host_port -i -e $db_username;"); + system("dropuser -U traffic_ops -h $host_ip -p $host_port -i -e $db_username;"); } sub showusers {