Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 934457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:00:15+00:00 2026-05-15T21:00:15+00:00

I write PSQL script and using variables (for psql –variable key=value commandline syntax). This

  • 0

I write PSQL script and using variables (for psql –variable key=value commandline syntax).

This works perfectly for top level scope like select * from :key, but I create functions with the script and need variable value inside them.

So, the syntax like

create function foo() returns void as
$$
declare
begin
    grant select on my_table to group :user;
end;
$$
language plpgsql;

fails at :user.

As far as I understand psql variables is a plain macro substitution feature, but it doesn’t process function bodies.
Are there any escaping syntax for such cases? Surrounding :user with $$ works regarding substitution, but psql fails at $$.

Are there any other way to do this besides standalone macro processing (sed, awk, etc)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:00:16+00:00Added an answer on May 15, 2026 at 9:00 pm

    PSQL SET variables aren’t interpolated inside dollar-quoted strings. I don’t know this for certain, but I think there’s no escape or other trickery to turn on SET variable interpolation in there.

    One might think you could wedge an unquoted :user between two dollar-quoted stretches of PL/pgSQL to get the desired effect. But this doesn’t seem to work… I think the syntax requires a single string and not an expression concatenating strings. Might be mistaken on that.

    Anyway, that doesn’t matter. There’s another approach (as Pasco noted): write the stored procedure to accept a PL/pgSQL argument. Here’s what that would look like.

    CREATE OR REPLACE FUNCTION foo("user" TEXT) RETURNS void AS
    $$
    BEGIN
            EXECUTE 'GRANT SELECT ON my_table TO GROUP ' || quote_ident(user);
    END;    
    $$ LANGUAGE plpgsql;
    

    Notes on this function:

    1. EXECUTE generates an appropriate GRANT on each invocation using on our procedure argument. The PG manual section called “Executing Dynamic Commands” explains EXECUTE in detail.
    2. The declaration of procedure argument user must be double quoted. Double quotes force it to be interpreted as an identifier.

    Once you define the function like this, you can call it using interpolated PSQL variables. Here’s an outline.

    1. Run psql --variable user="'whoever'" --file=myscript.sql. Single quotes are required around the username!
    2. In myscript.sql, define function like above.
    3. In myscript.sql, put select foo(:user);. This is where we rely on those single quotes we put in the value of user.

    Although this seems to work, it strikes me as rather squirrely. I thought SET variables were intended for runtime configuration. Carrying data around in SET seems odd.

    Edit: here’s a concrete reason to not use SET variables. From the manpage: “These assignments are done during a very early stage of startup, so variables reserved for internal purposes might get overwritten later.” If Postgres decided to use a variable named user (or whatever you pick), it could overwrite your script argument with something you never intended. In fact, psql already takes USER for itself — this only works because SET is case sensitive. This very nearly broke things from the start!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've just downloaded version 1.0.1, opened it up in Visual… May 16, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer Personally I like this corner detection algorithms manual. Also you… May 16, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer You want to find out the size of the machine… May 16, 2026 at 3:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a psql script that looks like this: -- first set of statements
I write a sh script (test.sh) like this: #!/bin/sh echo $@ and then run
I needed to write a script to enter multi-line input to a program (
I'm using psql to run a simple set of COPY statements contained in a
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
I write my debug information to a file using a separate thread. During startup,
i write some html with JS into a div. like this $(#picdiv).html(<a rel='lightbox' href='pic.jpg'><img
I am attempting to write a very simple rake task (and merge it into
I am setting up a project using Hibernate 3.3.1 GA and PostgreSQL 8.3 .
I am using java.sql.SQLData interface to map my java objects to Oracle database types.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.