PostgreSQL – Running .sql Files in Structured Programs

postgresqlstored-procedures

In oracle I could create .sql files and run them with a structure (like a stored procedure) with

begin

  declare

end;

I did some tests, because I need to run scripts to fix problems, not is necessary record it as a stored procedure/function on server, so like in Oracle, I want to run it once and check the result.

Not is possible run .sql files on postgres with CURSOR, declare, variables, etc.. , only directly on server with stored procedures/functions ?

Best Answer

The equivalent of Oracle's anonymous PL/SQL blocks are anonymous PL/pgSQL blocks in Postgres. These are started using the do statement:

do
$$
declare 
  ....
begin
  .... 
end;
$$