Re: Script generation through psql - Mailing list pgsql-admin

From David G. Johnston
Subject Re: Script generation through psql
Date
Msg-id CAKFQuwbL+S0kQiSPt_SzXbOYf8FtrPLLTnUUHq_y1ooyXOffDQ@mail.gmail.com
Whole thread Raw
In response to Script generation through psql  (Mauricio Fernandez <mmauricio.fernandez@gmail.com>)
Responses Re: Script generation through psql
List pgsql-admin
On Tuesday, October 21, 2025, Mauricio Fernandez <mmauricio.fernandez@gmail.com> wrote:

2. If in the psql script I want to pass variable values from an anonymous block to the rest of the script, how can I proceed?. For example

DO $$
DECLARE
  vl_id integer;
BEGIN
 -- vl_id is set inside the procedure
  call my_procedure(vl_id);
END $$;

You’d have to do something like:

Execute format(‘set script.varname=%L’, value)

Within the DO block then you can do:

Select current_value(‘script.varname’) as psql_var \gexec

Outside of it.

A temporary table works too.

David J.

pgsql-admin by date:

Previous
From: Roland Müller
Date:
Subject: Re: Script generation through psql
Next
From: Mauricio Fernandez
Date:
Subject: Re: Script generation through psql