I want to use the following function (as a trigger on a table's inserts):
CREATE OR REPLACE FUNCTION insert_authid_fn() RETURNS trigger AS $$
BEGIN
GRANT ALL ON DATABASE testdb TO NEW.username;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
I get the error:
ERROR: syntax error at or near "."
LINE 3: GRANT ALL ON DATABASE testdb TO NEW.username;
because New.username is value, rather than an identifier.
How can I achieve this GRANT?
syntactically:
CREATE OR REPLACE FUNCTION insert_authid_fn() RETURNS trigger AS $$
BEGIN
execute format ('GRANT ALL ON DATABASE testdb TO %I',NEW.username);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
But I doubt it is a good idea in general
Related
i made this trigger to prevent data modification in psql
but it prevent insert data too , how i can resolve this problem ?
CREATE OR REPLACE FUNCTION public.Check_Modification()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
BEGIN
RAISE EXCEPTION ' Sorry u can not modifict the data ' ;
END IF;
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION public.check_verwachte_bezetting()
OWNER TO postgres;
CREATE TRIGGER Check_Modification_trigger
BEFORE UPDATE
ON public.table
FOR EACH ROW
EXECUTE FUNCTION public.Check_Modification();
I wrote a SQL function to ALTER the SCHEMA Ownership. I am able to run the statement directly(without function) but getting error while running the ALTER statement within the function.
Function "test" created successfully:
CREATE OR REPLACE FUNCTION test.update_ownership(schemaname text) RETURNS text AS $$
DECLARE
BEGIN
ALTER SCHEMA schemaname OWNER TO postgresql;
END;
$$ LANGUAGE plpgsql;
Executing the function as:
select test.update_ownership('myschema')
I am getting an ERROR while executing the function i.e.:
ERROR: schema "schemaname" does not exist
CONTEXT: SQL statement "ALTER SCHEMA schemaname OWNER TO postgresql"
PL/pgSQL function collection.update_ownership(text) line 4 at SQL statement
SQL state: 3F000
You can't use parameters as identifiers, you have to use dynamic SQL for this. Generating the SQL is best done using the format() function to properly deal with identifiers.
CREATE OR REPLACE FUNCTION test.update_ownership(schemaname text)
RETURNS void
AS $$
BEGIN
execute format('ALTER SCHEMA %I OWNER TO postgresql', schemaname);
END;
$$ LANGUAGE plpgsql;
I am trying to create a function inside Postgres. For some reason my query works perfectly fine when I run it from inside the Query tool. Once I added the query to my function, I get the error
ERROR: syntax error at or near "DELETE"
Not sure what is the problem.
CREATE FUNCTION public.remove_email_header()
RETURNS pg_trigger
LANGUAGE 'plpgsql'
AS $BODY$
DELETE FROM public.spam_dictionary WHERE words LIKE '%FROM%';
$BODY$;
ALTER FUNCTION public.remove_email_header()
OWNER TO postgres;
Two Things to note.
A plpgsql Postgres function uses BEGIN END
The plpgsql should not be quoted.
CREATE FUNCTION remove_email_header()
RETURNS trigger
LANGUAGE plpgsql
AS $BODY$
BEGIN
DELETE FROM public.spam_dictionary WHERE words LIKE '%FROM%';
END
$BODY$;
I am attempting to create a trigger which will delete rows when a certain where clause criteria is met but it throws out an error. What am I doing wrong?
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
You must encapsulate your DELETE statement in a function, so the trigger will become:
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
So you must create a function as follow:
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
HERE you can find a brief guide on trigger creation
When a row is inserted or updated in a specific table (in this example it's the table called 'fpl'). How can I include the affected table and schema in the notification?
SQL as follows:
CREATE TRIGGER fpl_event
AFTER INSERT OR UPDATE ON fpl
FOR EACH ROW
EXECUTE PROCEDURE fpl_notify();
CREATE OR REPLACE FUNCTION fpl_notify()
RETURNS trigger AS $$
BEGIN
NOTIFY dbNotification, 'something got insereted in fpl!';
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
Update:
CREATE OR REPLACE FUNCTION fpl_notify() RETURNS trigger
AS
$$
BEGIN
EXECUTE format('notify dbNotification, ''%s''', TG_TABLE_SCHEMA);
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
read trigger special variables