473,750 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple, but VERYuseful enhancement for psql command - or am I missing something?

Ben
I'm designing a fairly involved database system. As part fo the process, I
use the \i [FILE] command a great deal. I set up fairly involved queries,
sometimes simply for the purpose of shortening column names so the output
is reasonable. For example:

SELECT longname AS abbr,othername as "V" FROM table WHERE how;

....a bunch of these can result in a single-line output on the console,
which is a lot easier to deal with than a dump of the actual field names
which wraps around and makes you scroll back and forth trying to line up
the names with the values.

Now, in my case, I'm dealing with specific orders. So the WHERE clause
might be:

....WHERE zorder=104788;

Which works fine. But, I have to edit the file every time I'm working with
a different order, which is repetative and annoying, something computers
are supposed to save us from. :)

However, you can't leave it out; \i [FILE] expects the query to be
complete, ready to go to the server. As far as I can tell.

So - how about a command to read a file into the input lines withOUT
sending it yet, so that its ready to type the last part, such as:

104788;

In other words, the file would end here:

....WHERE zorder=104788;
^
|
|
....then I could just type the number, hit enter, and off it would go.

Or even if it has to be complete, right now, you can use \i [FILE] and it
runs, but you can't edit the thing with the line review editing tools...
it shows the \i [FILE] command, not what the command read. That would work
too, even if it caused a dummy read the first time you used it.

Input, anyone?

--Ben

Nov 22 '05 #1
6 1726
Ben wrote:
I'm designing a fairly involved database system. As part fo the process, I
use the \i [FILE] command a great deal. I set up fairly involved queries,
sometimes simply for the purpose of shortening column names so the output
is reasonable. For example:

SELECT longname AS abbr,othername as "V" FROM table WHERE how;

...a bunch of these can result in a single-line output on the console,
which is a lot easier to deal with than a dump of the actual field names
which wraps around and makes you scroll back and forth trying to line up
the names with the values.

Now, in my case, I'm dealing with specific orders. So the WHERE clause
might be:

...WHERE zorder=104788;

Which works fine. But, I have to edit the file every time I'm working with
a different order, which is repetative and annoying, something computers
are supposed to save us from. :)

However, you can't leave it out; \i [FILE] expects the query to be
complete, ready to go to the server. As far as I can tell.

So - how about a command to read a file into the input lines withOUT
sending it yet, so that its ready to type the last part, such as:

104788;

In other words, the file would end here:

...WHERE zorder=104788;
^
|
|
...then I could just type the number, hit enter, and off it would go.

Or even if it has to be complete, right now, you can use \i [FILE] and it
runs, but you can't edit the thing with the line review editing tools...
it shows the \i [FILE] command, not what the command read. That would work
too, even if it caused a dummy read the first time you used it.

Input, anyone?

--Ben


I am not sure about this exactly, but a workaround could be using
temporary sequences. I use these a lot in some of my more involved DB
setup scripts.

So for instance in the top level file you have:

-------------------
CREATE SEQUENCE temp_zorder_num _seq;
SELECT setval('temp_zo rder_num_seq', 104788);

\i Somefile.sql

DROP SEQUENCE
-------------------

The in any \i file you can just use:

-------------------
INSERT INTO some_table (zorder_num, ...) VALUES
(currval('temp_ zorder_num_seq' ), ...);
-------------------

All you have to change is the setval at the top of the script. Make sure
you drop the sequences though ;-).
HTH

Nick

Nov 22 '05 #2
Better would be to match Oracle's sqlPlus feature, DEFINE.

The gist of which is that you can create a SQL statement with an "&" (or
other 'defined' character) in it. If DEFINE is ON, then the interpreter
prompts you for the value when it encounters the "&". After getting the
value it then processes the SQL statement.

Here is an example using sqlPlus:

SQL*Plus: Release 9.2.0.1.0 - Production on Fri Feb 27 14:11:18 2004

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

Connected to:
Oracle9i Release 9.2.0.1.0 - Production
JServer Release 9.2.0.1.0 - Production

SQL> select wdresourceid from wdresource where wdresourceid = &my_res_id;
Enter value for my_res_id: 615
old 1: select wdresourceid from wdresource where wdresourceid = &my_res_id
new 1: select wdresourceid from wdresource where wdresourceid = 615

WDRESOURCEID
------------
615

SQL> select wdresourceid from wdresource where wdresourceid = &my_res_id;
Enter value for my_res_id: 1
old 1: select wdresourceid from wdresource where wdresourceid = &my_res_id
new 1: select wdresourceid from wdresource where wdresourceid = 1

no rows selected

You also need the ability to switch off the DEFINE operation in case you
are using a SQL script which contains "&" characters which you don't want
the interpreter to treat as a define.

This would be a cool and useful feature, if it could be implemented in
psql...

John Sidney-Woollett

SQL>
Nick Barr said:
Ben wrote:
I'm designing a fairly involved database system. As part fo the process,
I
use the \i [FILE] command a great deal. I set up fairly involved
queries,
sometimes simply for the purpose of shortening column names so the
output
is reasonable. For example:

SELECT longname AS abbr,othername as "V" FROM table WHERE how;

...a bunch of these can result in a single-line output on the console,
which is a lot easier to deal with than a dump of the actual field names
which wraps around and makes you scroll back and forth trying to line up
the names with the values.

Now, in my case, I'm dealing with specific orders. So the WHERE clause
might be:

...WHERE zorder=104788;

Which works fine. But, I have to edit the file every time I'm working
with
a different order, which is repetative and annoying, something computers
are supposed to save us from. :)

However, you can't leave it out; \i [FILE] expects the query to be
complete, ready to go to the server. As far as I can tell.

So - how about a command to read a file into the input lines withOUT
sending it yet, so that its ready to type the last part, such as:

---------------------------(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 22 '05 #3
--- Nick Barr <ni***@chuckie. co.uk> wrote:
Ben wrote:
I'm designing a fairly involved database system.

As part fo the process, I
use the \i [FILE] command a great deal. I set up

fairly involved queries,
sometimes simply for the purpose of shortening

column names so the output
is reasonable. For example:

SELECT longname AS abbr,othername as "V" FROM

table WHERE how;

...a bunch of these can result in a single-line

output on the console,
which is a lot easier to deal with than a dump of

the actual field names
which wraps around and makes you scroll back and

forth trying to line up
the names with the values.
"man psql" is a good thing. Especially the section on
variables, in Ben's case. In summary, you can set a
variable in a psql session by "\set variablename
value", and refer to it in a query by ":variablename" .
This works for any value or identifier, i.e. psql
substitutes the variable value for the name before
sending the sql to the backend. Works when used in
script files too. I have used this a lot, and it's
handy.

Also, you may want to look at the "\x" command, and
its variations. This will output column name/value
pairs down the page, which can be handy for viewing
large records.

Now, in my case, I'm dealing with specific orders.

So the WHERE clause
might be:

...WHERE zorder=104788;

Which works fine. But, I have to edit the file

every time I'm working with
a different order, which is repetative and

annoying, something computers
are supposed to save us from. :)

However, you can't leave it out; \i [FILE] expects

the query to be
complete, ready to go to the server. As far as I

can tell.

So - how about a command to read a file into the

input lines withOUT
sending it yet, so that its ready to type the last

part, such as:

104788;

In other words, the file would end here:

...WHERE zorder=104788;
^
|
|
...then I could just type the number, hit enter,

and off it would go.

Or even if it has to be complete, right now, you

can use \i [FILE] and it
runs, but you can't edit the thing with the line

review editing tools...
it shows the \i [FILE] command, not what the

command read. That would work
too, even if it caused a dummy read the first time

you used it.

Input, anyone?

--Ben


I am not sure about this exactly, but a workaround
could be using
temporary sequences. I use these a lot in some of my
more involved DB
setup scripts.

So for instance in the top level file you have:

-------------------
CREATE SEQUENCE temp_zorder_num _seq;
SELECT setval('temp_zo rder_num_seq', 104788);

\i Somefile.sql

DROP SEQUENCE
-------------------

The in any \i file you can just use:

-------------------
INSERT INTO some_table (zorder_num, ...) VALUES
(currval('temp_ zorder_num_seq' ), ...);
-------------------

All you have to change is the setval at the top of
the script. Make sure
you drop the sequences though ;-).
HTH

Nick
---------------------------(end of
broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

_______________ _______________ ____
Do you Yahoo!?
Get better spam protection with Yahoo! Mail.
http://antispam.yahoo.com/tools

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #4
--- Nick Barr <ni***@chuckie. co.uk> wrote:
Ben wrote:
I'm designing a fairly involved database system.

As part fo the process, I
use the \i [FILE] command a great deal. I set up

fairly involved queries,
sometimes simply for the purpose of shortening

column names so the output
is reasonable. For example:

SELECT longname AS abbr,othername as "V" FROM

table WHERE how;

...a bunch of these can result in a single-line

output on the console,
which is a lot easier to deal with than a dump of

the actual field names
which wraps around and makes you scroll back and

forth trying to line up
the names with the values.
"man psql" is a good thing. Especially the section on
variables, in Ben's case. In summary, you can set a
variable in a psql session by "\set variablename
value", and refer to it in a query by ":variablename" .
This works for any value or identifier, i.e. psql
substitutes the variable value for the name before
sending the sql to the backend. Works when used in
script files too. I have used this a lot, and it's
handy.

Also, you may want to look at the "\x" command, and
its variations. This will output column name/value
pairs down the page, which can be handy for viewing
large records.

Now, in my case, I'm dealing with specific orders.

So the WHERE clause
might be:

...WHERE zorder=104788;

Which works fine. But, I have to edit the file

every time I'm working with
a different order, which is repetative and

annoying, something computers
are supposed to save us from. :)

However, you can't leave it out; \i [FILE] expects

the query to be
complete, ready to go to the server. As far as I

can tell.

So - how about a command to read a file into the

input lines withOUT
sending it yet, so that its ready to type the last

part, such as:

104788;

In other words, the file would end here:

...WHERE zorder=104788;
^
|
|
...then I could just type the number, hit enter,

and off it would go.

Or even if it has to be complete, right now, you

can use \i [FILE] and it
runs, but you can't edit the thing with the line

review editing tools...
it shows the \i [FILE] command, not what the

command read. That would work
too, even if it caused a dummy read the first time

you used it.

Input, anyone?

--Ben


I am not sure about this exactly, but a workaround
could be using
temporary sequences. I use these a lot in some of my
more involved DB
setup scripts.

So for instance in the top level file you have:

-------------------
CREATE SEQUENCE temp_zorder_num _seq;
SELECT setval('temp_zo rder_num_seq', 104788);

\i Somefile.sql

DROP SEQUENCE
-------------------

The in any \i file you can just use:

-------------------
INSERT INTO some_table (zorder_num, ...) VALUES
(currval('temp_ zorder_num_seq' ), ...);
-------------------

All you have to change is the setval at the top of
the script. Make sure
you drop the sequences though ;-).
HTH

Nick
---------------------------(end of
broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

_______________ _______________ ____
Do you Yahoo!?
Get better spam protection with Yahoo! Mail.
http://antispam.yahoo.com/tools

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5
"Ben" <re***@to-the-newsgroup.com> writes:
So - how about a command to read a file into the input lines withOUT
sending it yet, so that its ready to type the last part, such as:


I think \e can be used this way.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #6
Ben
I want to thank everyone for replying - that was really great.

My specific issue sounds like it would be best addressed using variables.
Thanks hugely for the pointer on that one.

Displaying things vertically isn't going to work for me in particular; you
lose too much display area that way. I need to be able to see multiple
results. I'll keep \x in mind for later, though.

\e allows you to edit an external file; this is essentially the problem,
rather than the solution: I don't want to edit it outside. I want it in
the query buffer where I can just hit ^ a few times, edit right there, and
let fly. I do this all the time now, the problem is I have to hand-enter
the query the first time, or I can't do it at all.

What I was suggesting was the ability to load an external file into the
query buffer (the line buffer, maybe I mean) such that (a) it is NOT
executed a'la \i, but can be picked up into the current line for editing
using the ^ arrow, changed as required, then fired off. This is a very
general solution to having things around that you can boilerplate, or
simply use because you know they're close, even if that wasn't the idea
originally.

The idea of using & seems cool enough, but since you can use SET and then
embed them in the external file, that'll do about the same thing, it seems
to me.

Thanks again for all the responses. You people rock.

--Ben

Nov 23 '05 #7

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

Similar topics

15
12345
by: Daniel Schuchardt | last post by:
Hi @ all, i'm sure there was a psql-function to transfere my Blob-Data to the server but I can't remember. I have a script like this : UPDATE xy SET z = lo_import('localpath_and_file'); but i want to execute this script from the client and so my blob-data is
7
37779
by: Willem Herremans | last post by:
I am developing a client application for postgreSQL in Tcl/Tk (see http://gborg.postgresql.org/project/pfm ). It mainly uses PgTcl or pgintcl. I don't have any problems with those, but I am also trying to call psql from my application for SQL statements typed directly by the user. I have used the Tcl command set psqlChannel
4
2154
by: Brendan Jurd | last post by:
Hello all, I just wanted to pass on some information about compatibility between the psql client and the postgres server. On a particular network, my workstation and the server are both debian boxes, but the workstation is on a more frequent upgrade schedule. The server is still running postgres 7.3.4, but my workstation has been upgraded with the latest (7.4.1) postgres client tools.
1
10876
by: Ennio-Sr | last post by:
Hi all! I'm writing a script that presents the user with a numbered lines menu, each line corresponding to a <case n> which executes a psql command. As the psql-commands are very similar to each other (all of them have the structure: 1.- psql mydb -x -c "SELECT * FROM tb_nm WHERE col_nm LIKE '%$k_r%'" ) I thought it was possible to shorten it, initializing a str with said command at the beginning of the script and limiting the...
2
1759
by: Russ Brown | last post by:
Hello, Today I tried connecting to my database locally via psql. I got the usual welcome & basic help messages, but it never got to the prompt: it just hung. So I checked top and the psql process was increasing in size at quite a rate (up to a gig in under 30 seconds). I'd been using psql with no problems only a couple of hours ago, and I haven't installed anything for at least a couple of days that I can think of.
2
4995
by: Jeffrey W. Baker | last post by:
I just noticed something unexpected when using psql. I had written a shell script to bulk-load some hundreds of files into a database, and move each file to success/ or failure/ directories depending on the exit status. The problem is psql exit status differs depending on if the SQL script is provided with -c or on STDIN. Try this for example: $ echo "your mom" | psql ERROR: syntax error at or near "your" at character 1
3
2227
by: Anony Mous | last post by:
Hi, I've run into a problem. I've had postgres V8 beta on my WinXP Pro machine for some time now, and it's been running great. Now, for some reason, I cannot issue any queries to the database via psql. See snippit below... ------------ C:\Program Files\PostgreSQL\8.0-beta1\bin>createdb -U postgres test CREATE DATABASE
4
17962
by: Kevin Murphy | last post by:
This is a tip for the record in case it helps somebody else in the future. I have an import script that relies on a stored procedure that runs as a trigger on inserts into a temporary table. The script looks like this: -- create table -- ... -- define procedure and trigger
5
5915
by: damacy | last post by:
hello, everyone. i am trying to write a program which executes SQL commands stored in ..sql files. i wrote a function called psql() whose contents look like the following. .... os.popen(command)
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
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
9396
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...
0
9256
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
8260
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
6804
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
6081
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();...
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.