473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interpolation of environment variables in SQL at runtime?

Hi all,

Question: is there any way to use environment variables (or something
similar) in my saved SQL code, and have them expanded at runtime?

I can't see a way to do this in the manuals, and a colleague tells me
it can't be done, but I wanted to check, because if this _can_ be done
it will make my life significantly more optimal.

In more detail:

- I deal with postgres in one of two ways:

1. From an interactive emacs session, where I edit code in one
buffer, and send it to another one (in SQL mode) for
execution - which I imagine is an entirely familiar idea to
many readers of this list.

2. Using python programs which have SQL embedded directly in
them.

- Some of this code refers to files on disk, using absolute
pathnames. In particular, there are a lot of \i directives (ie to
include another .sql file).

- This code is shared with colleagues, and of course people have
different paths to the files involved.

- So, it would be really REALLY nice if I could rewrite things so
that:

\i /home/foo/some/path/to/root_of_files/some_file.sql

would be:

\i $ROOT_OF_FILES/some_file.sql

with $ROOT_OF_FILES referring to an environment variable, which is
then replaced with the value of that variable at the time that the
database executes the code. Then everybody who uses this code
just needs to ensure that ROOT_TO_FILES is set appropriately for
them, and everything works fine.

Hopefully what I'm asking is fairly obvious. But is it possible? I
can imagine some fairly unwiedly ways to do this involving
preprocessing, but would involve jumping through lots of hoops, and
I'd hope for a cleaner solution. Environment variables seems the
obvious "Unixy" approach...

Surely people have hit this problem before? How's it solved?

Many thanks for any advice!

-Andy

--
Andy Gimblett
Computer Science Department
University of Wales Swansea
http://www.cs.swan.ac.uk/~csandy/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #1
4 4956
Andy Gimblett <A.**********@s wansea.ac.uk> writes:
Question: is there any way to use environment variables (or something
similar) in my saved SQL code, and have them expanded at runtime?


Only by preprocessing the script. Have you considered something like

cat <<EOF
.... WHERE $foo = ...
EOF | psql ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Andy Gimblett <A.**********@s wansea.ac.uk> writes:
Question: is there any way to use environment variables (or something
similar) in my saved SQL code, and have them expanded at runtime?


Only by preprocessing the script. Have you considered something like

cat <<EOF
.... WHERE $foo = ...
EOF | psql ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3
Andy Gimblett wrote:
Hopefully what I'm asking is fairly obvious. But is it possible? I
can imagine some fairly unwiedly ways to do this involving
preprocessing, but would involve jumping through lots of hoops, and
I'd hope for a cleaner solution. Environment variables seems the
obvious "Unixy" approach...

Surely people have hit this problem before? How's it solved?


See plr_environ() in PL/R's pg_userfunc.c. You could probably rip it out
and use it standalone with reasonable ease (or perhaps modify it to
return a single requested environment variable).

Use it like this:

select value from plr_environ() where name = 'MANPATH';
value
---------------------------
/usr/local/pgsql-dev/man:
(1 row)

Joe

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #4
Andy Gimblett wrote:
Hopefully what I'm asking is fairly obvious. But is it possible? I
can imagine some fairly unwiedly ways to do this involving
preprocessing, but would involve jumping through lots of hoops, and
I'd hope for a cleaner solution. Environment variables seems the
obvious "Unixy" approach...

Surely people have hit this problem before? How's it solved?


See plr_environ() in PL/R's pg_userfunc.c. You could probably rip it out
and use it standalone with reasonable ease (or perhaps modify it to
return a single requested environment variable).

Use it like this:

select value from plr_environ() where name = 'MANPATH';
value
---------------------------
/usr/local/pgsql-dev/man:
(1 row)

Joe

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5204
by: Charles Banas | last post by:
I'm not sure if this is the right place to ask about this, but I've seen several posts in the past regarding Akima's Bivariate Interpolations routines, and i'm wondering if someone can give me some ideas or PD code I can put to use right away. At the moment, I'm maintaining a contour calculation and plotting program for radio wave propagation studies. This program uses a number of routines written by and for the FCC in Fortran in 1976...
3
3912
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and it shall return the y value to any x value given. e.g. p1: (0,0) p2 (1000,50) y value to calculate at x=600
13
2206
by: John Bowman | last post by:
Hi All, I've got a simple wrapper static test method on a class to expand the environment variables on a specified string: public static string ExpandEnvironmentStr(string Str) { return Environment.ExpandEnvironmentVariables(Str); }
0
982
by: Andy Gimblett | last post by:
Hi all, Question: is there any way to use environment variables (or something similar) in my saved SQL code, and have them expanded at runtime? I can't see a way to do this in the manuals, and a colleague tells me it can't be done, but I wanted to check, because if this _can_ be done it will make my life significantly more optimal. In more detail:
6
3803
by: Fuzzyman | last post by:
Hello all, I would like to set a Windows Environment variable for another (non-child) process. This means that the following *doesn't* work : :: os.environ = value In the ``win32api`` package there is a ``GetEnvironmentVariable``
5
3813
by: xandra | last post by:
i understood the concept of interpolation search. but i couldn't understand what would be the steps for that search. for example, if i'm searching for J in this file A A B E F H J M N N N N O P P P P P R R R R T T T Z Z Z Z Z Z Z i know that i have to look in the begining of this file. but what is the steps to find j/ thank you for your help
2
1541
by: Colin McKinnon | last post by:
Hi all, I'm wondering if this is possible with PHP: $query = "SELECT * FROM table WHERE afield='$something'"; has quite a different meaning from $query = 'SELECT * FROM table WHERE afield=\'$something\'';
0
1291
by: MonkeeSage | last post by:
There are several string interpolation functions, as well as string.Template. But here's yet another. This one emulates ruby's inline interpolation syntax (using #{}), which interpolates strings as well as expressions. NB. It uses eval(), so only use it in trusted contexts! import sys, re def interp(string): locals = sys._getframe(1).f_locals globals = sys._getframe(1).f_globals
10
12809
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem involving variable scope in JavaScript.) I have a lot of code that generates HTML on the fly. This code has tags with id attributes derived from variables. A small example: blah('<span id="' + dev + '_' + mod + '">...</span>');
5
4468
by: August Karlstrom | last post by:
Hi, Does anyone know the correct syntax to interpolate a class variable, $x say, inside a string? I tried "{self::$x}" but it produces the string {self::x}
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.