Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 June PostgreSQL: Use PSQL command line variable to make your SQL Queries Dynamic

PostgreSQL: Use PSQL command line variable to make your SQL Queries Dynamic

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing tips like how to use a psql session variable to make your queries more dynamic in PostgreSQL.

As a DBA, we prepared lots of .sql files in our server for different purposes like check DB stats, get tables size, get object information.

We are executing this scripts (.sql files) many times, but every time we have to change the schema name parameters because we are checking for different schemas and we hard coded the schema values in the SQL scripts.

I found one solution to resolve this problem, that is PSQL Command line variable. You can declare the variable in the PSQL Session and can use for that session.

The benefit of this is, you do not require to update .sql files for again and again, and multiple people can use same SQL script at a time for different schemas.

Note: As this is a session variable so whenever you connect to PSQL, you have to declare this variable.

Check the below demonstration:

Step 1: Connect to PSQL

1
root-> psql -d database_name

Step 2: Create a session variable

1
psql-> \set SchemaVariable '\'my_schema_1\''

Step 3: Embed that variable in your SELECT script of .sql file

1
2
3
4
5
6
select
tablename
,attname
,avg_width
from pg_stats
where schemaname = :SchemaVariable;

If you are using a list of schemas, you can also assign multiple schema names to a session variable.

Declare session variable for multiple values:

1
\set SchemaVariable '\'my_schema_1\',\'my_schema_2\''

Use session variable in IN clause:

1
2
3
4
5
6
select
tablename
,attname
,avg_width
from pg_stats
where schemaname in (:SchemaVariable);

Jun 19, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find first 100 Prime numbers in SQL ServerSQL Puzzle: SQL Advance Query - To find total number of Sundays in a given Year

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 19, 2017 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Monitoring, PostgreSQL Performance Tuning, PostgreSQL Programming, PostgreSQL Tips and Tricks, psql, session variable
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....