Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 08:24 PM
Michelle Konzack
 
Posts: n/a
Default How to query pgsql from a BASH script ?

Hello *,

I am puzzeling around, how to query a postgresql from a BASH script.
Generaly it must do nothing else as

1) Get VALUEs from a DB/TABLE
e.g.
program --db pgsql.sld.tld --search "$COLNAME,$VAL" \
--get "COL1,COL3,COL4,..."

2) Write one or more new VALUEs in the DB/TABLE
e.g.
program --db pgsql.sld.tld --search "$COLNAME,$VAL" \
--set "COL1:VAL1,COL3:VAL3,..."

3) Remove ROWS from the DB/TABLE
e.g.
program --db pgsql.sld.tld --search "$COLNAME,$VAL" --remove


Curently I have only a sulution with a text/plain file but
there is a problem with locking and the file is already
180 kByte, which mean grep/cut/sed are to slow for it.

Under "heavy" load I need to access the database around
1-3 times (maybe in the future more) per second.

Any suggestions ?

Greetings
Michelle

--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
Michelle Konzack Apt. 917 ICQ #328449886
50, rue de Soultz MSM LinuxMichi
0033/3/88452356 67100 Strasbourg/France IRC #Debian (irc.icq.com)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCT/qvC0FPBMSS+BIRAh2oAKCIP3BYsAxFkDQFpGhEAqdNjs35VgCg tSiZ
ldrzTdusdjQ0Whmxm1q2QOE=
=xTCD
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 08:24 PM
Pavel Stehule
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

On Sun, 3 Apr 2005, Michelle Konzack wrote:

> Hello *,
>
> I am puzzeling around, how to query a postgresql from a BASH script.
> Generaly it must do nothing else as
>


Hello,

There is more solutions. The best is pgbash (pgbash is patch for bash)
http://www.psn.co.jp/PostgreSQL/pgbash/index-e.html

and last two months there somebody released new functions for bash and
postgresql but I can't remember

/usr/local/bin/pgbash
connect to testdb011;
BEGIN;
DECLARE c CURSOR FOR
SELECT name FROM names WHERE name LIKE '$1%';
lines=1
FETCH IN c INTO :name;
while [ $SQLCODE -eq $SQL_OK ]; do
if [ $lines -gt $2 ] ; then
break
fi
echo $name
let "lines+=1"
FETCH IN c INTO :name;
done
END;
disconnect all;

or

#!/usr/local/bin/pgbash
connect to template1;

set option_header=off;
set option_bottom=off;
set option_alignment=off;
set option_separator=;

dblist=`SELECT d.datname FROM
pg_catalog.pg_database d LEFT JOIN pg_catalog.pg_user u
ON d.datdba = u.usesysid WHERE u.usename LIKE '$1';`

if [ "$dblist" != "" ]; then
echo "$dblist" | while read db; do
echo "Remove database $db"
DROP DATABASE \"$db\";
done
fi
disconnect all;

regards
Pavel Stehule


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 08:24 PM
Sean Davis
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

Michelle,

There may be other answers for this, but if you need to connect 2-3 times
per second, you probably need a persistent connection which, as far as I
know, can't be obtained (and maintained) from bash. Is there a reason not
to do this from the server side or even from a standard client-side language
like perl, java, or C?

Sean

----- Original Message -----
From: "Michelle Konzack" <linux4michelle@freenet.de>
To: <pgsql-general@postgresql.org>
Sent: Sunday, April 03, 2005 10:16 AM
Subject: [GENERAL] How to query pgsql from a BASH script ?




---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 08:24 PM
Michelle Konzack
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

Am 2005-04-03 10:41:06, schrieb Sean Davis:
> Michelle,
>
> There may be other answers for this, but if you need to connect 2-3 times
> per second, you probably need a persistent connection which, as far as I
> know, can't be obtained (and maintained) from bash. Is there a reason not
> to do this from the server side or even from a standard client-side
> language like perl, java, or C?


First: I have no clue about perl and java.
Second: I use C, but never used in conjunction with a database

I the datsbase should queried from, e.g., a procmailrc and check
for IP addresses and much more. Most TABLES has only 2 or 3 COLS

The problem are the ROWS of the database.

So I am searching for a simpel solution to access the DB from BASH

OK, I have already codes stuff in php and the RETVAL was simpel
"VAL1 VAL2 VAL3" which I can cut into:

RETVAL=`php pg_query.php $SERVER $DB $TABLE $COL $SEARCH`
if [ $? == "1" ] ; then exit 1 ; fi
VAL1=`echo $RETVAL |cut -d " " -f1`
VAL2=`echo $RETVAL |cut -d " " -f2`
VAL3=`echo $RETVAL |cut -d " " -f3`

where the pg_query.php has serched only in the
given $COL and returnd the whole $ROW as RETVAL.

I was thinkg that such tool already exist in C.

But if you have a DB of 10.000 lines, the above examle will be faster
then a BASH solution with a file like

RETVAL=`grep "^$SEARCH" $DB_FILE`
if [ $? == "1" ] ; then exit 1 ; fi
VAL1=`echo $RETVAL |cut -d " " -f1`
VAL2=`echo $RETVAL |cut -d " " -f2`
VAL3=`echo $RETVAL |cut -d " " -f3`

and pgsql can be updated concurently, a $DB_FILE not.

Oh yes, from time to time a have realy need for a DB even in stupid
BASH scripts because it makle the life easier.

And the other think is, the first $COL is every time UNIQ.

> Sean


Greetings
Michelle

--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
Michelle Konzack Apt. 917 ICQ #328449886
50, rue de Soultz MSM LinuxMichi
0033/3/88452356 67100 Strasbourg/France IRC #Debian (irc.icq.com)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCUA0XC0FPBMSS+BIRAtfeAJ47HJRgYQ6bOpGBUfekat dU2G+mFgCeITVs
eohEusCzyaYmKrJD3v2jZA8=
=xq6g
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 08:24 PM
Michelle Konzack
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

Hello Pavel,

Am 2005-04-03 16:36:47, schrieb Pavel Stehule:

> Hello,
>
> There is more solutions. The best is pgbash (pgbash is patch for bash)
> http://www.psn.co.jp/PostgreSQL/pgbash/index-e.html


Wow... realy cool.
Unfortunatly I can not get the bash-2.0.5a tarball - ServerError
I will try to apply the patch to the version in Debian WOODY...

> and last two months there somebody released new functions for bash and
> postgresql but I can't remember


<example>

I was reading the Website, FAO and Examples...
But some things are confusing.

I need only a simple returnvalue of a FULL_ROW (one String, where $FIELD
are seperated by whitespace) from a SEARCH in the first $COL (uniq).

So if I understand it right, if I have a table like

Table: ip_table
ip | ctime | atime
----------------+------------+------------
aaa.bbb.ccc.ddd | 1234567890 | 2345678901
eee.fff.ggg.hhh | 3456789012 | 4567890123

and I need only

CONNECT TO localhost USER michelle.konzack;
RETVAL=`SELECT ctime, atime FROM ip_table WHERE ip = $SEARCH;`

?

> regards
> Pavel Stehule


Greetings
Michelle

--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
Michelle Konzack Apt. 917 ICQ #328449886
50, rue de Soultz MSM LinuxMichi
0033/3/88452356 67100 Strasbourg/France IRC #Debian (irc.icq.com)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCUCOEC0FPBMSS+BIRAsbLAJ9gkGsGMk9uauUoYU3vl5 jAA1TzbgCdGC0L
2+3QgzX8E4dpnNNoFwyHTtg=
=7MEG
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-08-2008, 08:24 PM
Adrian Klaver
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

On Sunday 03 April 2005 07:16 am, Michelle Konzack wrote:
> Hello *,
>
> I am puzzeling around, how to query a postgresql from a BASH script.
> Generaly it must do nothing else as
>

I recently came across this program-ShellSQL. I haven't had time to try it,
just read through the documentation. It may be able to do what you want.
http://www.edlsystems.com/shellsql/

--
Adrian Klaver
aklaver@comcast.net

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

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-08-2008, 08:24 PM
Pavel Stehule
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

> So if I understand it right, if I have a table like
>
> Table: ip_table
> ip | ctime | atime
> ----------------+------------+------------
> aaa.bbb.ccc.ddd | 1234567890 | 2345678901
> eee.fff.ggg.hhh | 3456789012 | 4567890123
>
> and I need only
>
> CONNECT TO localhost USER michelle.konzack;
> RETVAL=`SELECT ctime, atime FROM ip_table WHERE ip = $SEARCH;`
>

test=# select * from foo;
a | b
----+----
10 | 10
10 | 20

pgbash> retval=`select a,b from foo;`
pgbash> echo $retval
a | b ----+---- 10 | 10 10 | 20 (2 rows)

I remeber other project

http://www.edlsystems.com/shellsql

bye

Pavel Stehule



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-08-2008, 08:25 PM
Martijn van Oosterhout
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

On Sun, Apr 03, 2005 at 11:17:41PM +0200, Pavel Stehule wrote:
> test=# select * from foo;
> a | b
> ----+----
> 10 | 10
> 10 | 20
>
> pgbash> retval=`select a,b from foo;`
> pgbash> echo $retval
> a | b ----+---- 10 | 10 10 | 20 (2 rows)


The way I usually do it in scripts is:

psql '-F<tab>' -A -t -c "query"

If there's only one field output you can drop the -F.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFCUVWFY5Twig3Ge+YRAjVzAJ4wBiJSkTkBT2B82uf75u 9M7H36dgCg29ZJ
4whby4AoQ6oyE9cPkslxinE=
=0zMa
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-08-2008, 08:25 PM
Michelle Konzack
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

Am 2005-04-03 23:17:41, schrieb Pavel Stehule:

> I remeber other project
>
> http://www.edlsystems.com/shellsql


Thanks I will heck it out immediatly...

"pgbash" is a real killer but I can not apply
the patches becasue it ends in a error.

> bye
>
> Pavel Stehule


Greetings
Michelle

--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
Michelle Konzack Apt. 917 ICQ #328449886
50, rue de Soultz MSM LinuxMichi
0033/3/88452356 67100 Strasbourg/France IRC #Debian (irc.icq.com)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCUVsmC0FPBMSS+BIRAgedAKCGlskhiyFYPzHw2t23hG Gyn20u/ACfRKpW
/YYaklb+HUAQkwv+zGqAEJg=
=cCMB
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-08-2008, 08:25 PM
Pavel Stehule
 
Posts: n/a
Default Re: How to query pgsql from a BASH script ?

>
> Thanks I will heck it out immediatly...
>
> "pgbash" is a real killer but I can not apply
> the patches becasue it ends in a error.
>


pgbash is nice, but without actualisation. You have to use good version of
bash,

[stehule@stehule stehule]$ pgbash --version
GNU bash, version 2.05a.0(1)-release (i586-pc-linux-gnu)
Copyright 2001 Free Software Foundation, Inc.

Pavel


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608