-
Is it possible execute SQL in supabase API? I have created a query to group many columns in my table, but I cannot do it using your API, I didn't find any example to extract years or months from dates and neither an example to group columns using SUM or COUNT for example. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 27 replies
-
We do not allow direct execution of SQL from our APIs. There are two possible solutions:
The latter would be the simpler solution. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Thank you for posting this. Is there anybody who is able to clarify what it means to "generate a view"? |
Beta Was this translation helpful? Give feedback.
-
Any udpate on this to use |
Beta Was this translation helpful? Give feedback.
-
Can Supabase team just create wrapperJs to write raw SQL. I am moved from FireBase and I am just lost why it’s hard to achieve raw SQL on Supabase |
Beta Was this translation helpful? Give feedback.
-
Anyone figured out how to execute DYNAMIC queries? I'm generating SQL queries using a LLM so I can't hard code the query in a function, perfect solution would be to have a function that let the user execute read only queries (don't care about RLS stuff) CREATE OR REPLACE FUNCTION execute_raw_select_sql(query text)
RETURNS SETOF RECORD AS $$
DECLARE
forbidden_pattern text[] := ARRAY['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'ALTER', 'DROP', 'CREATE', 'REVOKE', 'GRANT'];
BEGIN
-- IF EXISTS (SELECT 1 FROM unnest(forbidden_pattern) pattern WHERE upper(query) LIKE '%' || upper(pattern) || '%') THEN
-- RAISE EXCEPTION 'Query contains forbidden operations';
-- END IF;
-- TODO: the if is broken somehow
RETURN QUERY EXECUTE query;
END;
$$ LANGUAGE plpgsql; Getting this
Any help appreciated 🙏 My workaround is to generate and execute JS code instead :/ pref SQL |
Beta Was this translation helpful? Give feedback.
-
you can create a function that allows you to pass your raw SQL query as a parameter. Just be aware of the security implications it might bring. CREATE OR REPLACE FUNCTION execute_dynamic_sql(sql_command TEXT)
RETURNS void AS $$
BEGIN
EXECUTE sql_command;
END;
$$ LANGUAGE plpgsql; and to run:
mote that the function returns void, meaning nothing. if you want the function to return some data you need to change the function return from void to something you need like a JSON response |
Beta Was this translation helpful? Give feedback.
We do not allow direct execution of SQL from our APIs. There are two possible solutions:
The latter would be the simpler solution. Hope this helps!