This is a very common task for a DBA(Database Administrator) and it can be time consuming in some cases.
So in this article we will see how we can build a User Defined Procedure in HP Vertica to clone a Database user. We have to user User Defined Procedure due to the lack of programmability features offered by Vertica(Vertica was build for super smooth Sql stuff).
So first we will go over the scripts we will use and them see how we can call them from Database as UDP(User Defined Procedure).
1 - First Script:
. /home/dbadmin/.profile
this file will have to contain the user and pasword used for the porcedure to run and execute(in most cases will be dbadmin user).
we will use this "trick" to avoid text/hard coded password when running scripts against the database
Note:
this file is of extreme importance !!! so make sure only the owner has access to it.
this script will generate the final SQL that will contain the new user SQL definition.
note that we have 3 parameter that we pass to this script $1(new_user),$2(password),$3(old_user), they will be echoed into the /tmp/create_user.sql.
3 - Third Script
Script execution.
this part is pretty obvious, will execute the content of the /tmp/create_user.sql script
Full script
this will hold all the described script above.
Now lets go and install this script as a UDP, so we can call it as a Vertica SQL action.
1 - We need to create the script into the procedure folder of the Vertica Database.
The Folder is located at /vertica/catalog/database_name/procedures/, the folder naming convention is specific to each database.
after you create the script , lets call it clone_user.sh you need to change the privilege definition on it.
2 - Install the store Procedure on the Vertica Cluster.
you must have the script on all nodes that are part of the cluster.
must be logged as dbamin user.
3 - Install the Procedure on your Database
must be logged as dbamin user.
Lets the the procedure in action
I have a user called test_user that has the following access:
and definition:
Lets run our clone_user procedure and create a new user called test_user_clone
the output of the procedure is:
Lets check the new user definition:
And grants:
... i guess it worked boys !!!
I hope this was useful and if you have comments or suggestions please fell free to leave them here.
Note:
Use this script at your own RISK, so make sure you test it in DEV before you break your production :)