I want to create a dbms_job as a sys user and the schema user of this job should be schemaA.
I have tried alter session set current_schema=schemaA, but that did not work. I still get sys as the schema user.
DECLARE jobno numeric;
Next_Date Date;
next_interval varchar2(20);
BEGIN
select SYSDATE+1 into Next_Date from dual;
dbms_job.submit(jobno,'<pl/sql code>',next_date,'FREQ=minutely;BYMINUTE=0,10,20,30,40,50;BYSECOND=0'); COMMIT; END;
/
Related
The below script runs but does not create the user when database name matches?
i just cannot spot what i am doing wrong here.
DO
$do$
DECLARE
lc_s_db_name CONSTANT VARCHAR(30) := 'dev';
lv_db_name VARCHAR(100);
BEGIN
select datname into lv_db_name from pg_catalog.pg_database;
IF lv_db_name in (lc_s_db_name )
THEN
create role monitor LOGIN PASSWORD 'monitor';
END IF;
END
$do$;
Your select query picks one database name of the list of all databases. That's not necessarily the one you are connected to.
To get the name of the database you are connected to, use the function current_database()
DO
$do$
DECLARE
lc_s_db_names CONSTANT text[] := array['dev', 'dev2'];
BEGIN
IF current_database() = any(lc_s_db_names)
THEN
create role monitor LOGIN PASSWORD 'monitor';
END IF;
END
$do$;
This is a symptom of database and user names being different between my dev/staging/live environments, but is there a way to GRANT permissions to a user, determined by some kind of sub-query?
Something like this (not valid syntax):
GRANT UPDATE (my_column) ON my_table TO (SELECT CASE current_database()
WHEN 'account-dev' THEN 'c-app'
WHEN 'account-staging' THEN 'x-app'
WHEN 'account-live' THEN 'a-app'
END);
Use psql and its wonderful \gexec:
SELECT format(
'GRANT UPDATE (my_column) ON my_table TO %I;',
CASE current_database()
WHEN 'account-dev' THEN 'c-app'
WHEN 'account-staging' THEN 'x-app'
WHEN 'account-live' THEN 'a-app'
END
) \gexec
Alternatively, you can write a DO statement that uses EXECUTE to execute a dynamic statement constructed as above.
I want to write a small script to grant permissions. The script works if I type in the user directly into each query but it's more efficient to use a variable but I cannot find what type to declare it as.
DO $$
DECLARE
usr ??? := myuser;
BEGIN
GRANT SELECT, INSERT, UPDATE, DELETE
ON ALL TABLES IN SCHEMA public
TO usr;
GRANT ALL PRIVILEGES ON SCHEMA public to usr;
END $$
You need dynamic SQL for that:
DO
$$DECLARE
usr text := 'myuser' ;
BEGIN
EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE '
'ON ALL TABLES IN SCHEMA public '
'TO %I',
usr);
END;$$;
The second statement works similarly.
I am going to update the table of huge record from one DB to another DB.
Here I'm using a cursor function:
CREATE OR REPLACE FUNCTION Drug("Drug" text)
RETURNS void AS
$BODY$
DECLARE
curs refcursor;
rec record;
BEGIN
OPEN curs FOR EXECUTE 'SELECT * FROM ' || quote_ident("Drug") FOR UPDATE;
LOOP
FETCH NEXT FROM curs INTO rec;
EXIT WHEN rec IS NULL;
RAISE NOTICE '%', rec."Id";
EXECUTE format('update statement with dblink', tbl)
USING rec.ctid;
END LOOP;
END
$BODY$ LANGUAGE plpgsql;
Is this correct?... or any other...
Please suggest...
What I know dblink doesn't support cursors - cursors cannot be parameter of query (cursors in PostgreSQL are very careful, usually limited to transaction). So this idea is wrong. You can generate row updates - but it will be slow.
What you can do. You can use FDW API (foreign data wrappers API) and create permanent link (foreign table to second database). Then you can send a usual UPDATE statement to second database.
I have two databases db1 and db2. db1 is source database, db2 is target database:
-- all is executed on db2
CREATE EXTENSION postgres_fdw;
CREATE SERVER db1 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'db1');
CREATE USER MAPPING FOR pavel
SERVER db1 OPTIONS (user 'pavel');
CREATE FOREIGN TABLE db1_source(a int, b int)
SERVER db1 OPTIONS (table_name 'source');
Now I have a FDW table db1_source and I can do UPDATE:
db2=# UPDATE target SET b = db1_source.b
FROM db1_source WHERE target.a = db1_source.a;
This is most effective way how to do UPDATE based on data in other database
I am in the middle of converting an existing SQL Server 2005 DB into a PostgreSQL 9.0 DB.
Everything works fine until now. I want to translate a SQL trigger into PostgreSQL but I have a problem with the trigger function.
I don't know how to implement the temp table inserted in the PostgreSQL syntax. In SQL Server the inserted table exists but not in PostgreSQL. Any ideas?
My code (PostgreSQL):
CREATE OR REPLACE FUNCTION func_co_insert()
RETURNS trigger AS
$BODY$begin
declare
aa bigint;
begin
select aa = co_id from inserted;
update com03 set co_creationdate = CURRENT_TIMESTAMP,
co_creationby = USER where co_id = aa;
end;
end;
Here the code of the trigger body of the SQL Server 2005 code
begin
declare #aa bigint;
select #aa = se_id from inserted;
update server set se_creationdate = CURRENT_TIMESTAMP , se_creationby = USER where se_id = #aa;
end;
thanks
Chris
The default in PostgreSQL is a row level trigger (as opposed to SQL Server where it's a statement level trigger), so there is no need for an "inserted" table to select from.
The new and old values can be accessed using the keyword new and old (old does not exist for an insert trigger).
In your case the statement would simply be:
update com03
set co_creationdate = CURRENT_TIMESTAMP,
co_creationby = CURRENT_USER
where co_id = new.co_id;
No need to "select from inserted".
This assumes the trigger is not firing for the table com03. If your trigger fires for com03 (which you didn't tell us), then it' even easier:
new.co_creationdate := current_timestamp;
new.co_creationby := current_user;
For details please refer to the manual: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html
That page also contains an example which does exactly what you are trying to achieve