I am trying to automate a few SQL queries on Redshift, that are 'triggered' from a user permission.
The queries simply create a view of the table with the filter of a customer_id, create a user with the customer_id and give permission for that customer_id to read the view of the table. This prevents different customers seeing eachothers data and this works nicely when I implemented this manually.
The issue I face is automating this as I am manually doing this in Redshift at the moment. I want to automate this given
What I considered:
Any thoughts or suggestions on this please?
Rather than using a different View for each user, you should create one View that uses current_user
.
This will return the username of the currently logged-in user. Thus, the query can use the username in a Where clause to limit the rows to those that match their identity.
It would either need a column that contains their username, or the View would need to join a table that can match the username to the customer_id
.
It would be something like:
CREATE VIEW foo
AS
SELECT
...
FROM table
JOIN users USING (customer_id)
WHERE users.username = current_user
You may use lazy pattern for creating view and permissions. In API-lambda:
if user is not created, create it using SQL query, go to step #4
If user permission is not created, create it using SQL query, go to step #1
You have to switch connection between superuser and customer_id user for first request and then it will be using customer_id user for all subsequent requests.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.