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 8459079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:14:58+00:00 2026-06-10T13:14:58+00:00

psql has a construct for passing named arguments: psql -v name=’value’ which can then

  • 0

psql has a construct for passing named arguments:

psql -v name='value'

which can then be referenced inside a script:

SELECT :name;

which will give the result

 ?column?
----------
 value
(1 row)

During development, I need to drop and recreate copies of the database fairly frequently, so I’m trying to automate the process. So I need to run a query that forcibly disconnects all users and then drops the database. But the database this operates on will vary, so the database name needs to be an argument.

The problem is that the query to disconnect the users requires a string (WHERE pg_stat_activity.datname = 'dbname') and the query that drops requires an unquoted token (DROP DATABASE IF EXISTS dbname). (Sorry. Not sure what to call that kind of token.)

I can use the named argument fine without quotes in the DROP query, but quoting the named argument in the disconnect query causes the argument to not be expanded. I.e., I would get the string ':name' instead of the string 'value'.

Is there any way to turn the unquoted value into a string or turn a string into an unquoted token for the DROP query? I can work around it by putting the disconnect and DROP queries in separate scripts and passing the argument in with quotes to the disconnect and without quotes to the DROP, but I’d prefer they were in the same script since they’re really two steps in a single process.

  • 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-06-10T13:15:00+00:00Added an answer on June 10, 2026 at 1:15 pm

    Use:

    ... WHERE pg_stat_activity.datname = :'name'

    Note the placement of the colon before the single quote.
    The manual:

    If an unquoted colon (:) followed by a psql variable name appears
    within an argument, it is replaced by the variable's value, as
    described in SQL Interpolation below. The forms
    :'variable_name' and :"variable_name" described there work as well.

    And:

    To quote the value of a variable as an SQL literal, write a colon
    followed by the variable name in single quotes.

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

Sidebar

Related Questions

I write PSQL script and using variables (for psql --variable key=value commandline syntax). This
I can run commands like vacuumdb, pg_dump, and psql just fine in a script
I have 3 tables Person, Names, and Notes. Each person has multiple name and
I have a psql script that looks like this: -- first set of statements
I need to run bin/psql on the command line (or script) and print its
I have a weird problem. I can connect to a remote host using psql
If I do a which psql and am seeign the path as, usr/bin/psql It
It seems that psql has no branching, and doesn't support PL/pgSQL blocks either. How
I want to launch psql.exe from an application. The user locates the script, so
I want to create a PSQL function which returns a table like result. My

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.