Version 4 of PostgreSQL

Updated 2002-08-30 10:24:47

This is a (full ACID) SQL database and it is OpenSource. It comes with binding for many languages, one for Tcl. The database has its home at http://www.postgresql.org


This Tcl binding comes as a (shared and/or static) library and on a Linux-System you can easily build a package from that. You only need to create a directory under, say, /usr/lib/tcl (replace this by a path searched by your local Tcl/Tk installation), place a copy of the library there, and go ahead. Here's an example:

 > cd /usr/lib/tcl8.3
 > mkdir postgresql
 > cp /usr/local/pgsql/lib/libpgtcl.so.2.2 postgresql/libpgtcl.so
 > cd postgresql
 > tclsh
 % pkg_mkIndex /usr/lib/tcl8.3/postgresql *.so
 % exit

This produced a pkgIndex.tcl file with all needed information to use the package. I then use the package with a simple

 package require Pgtcl

in my applications.


I found, that the syntax of the Pgtcl commands are not the Tcl way. So I wrote a little wrapper around the commands provided by the Pgtcl package to make them more tcl-like:

 proc pg {cmd args} {
   switch $cmd {
      connect {pg_connect [lindex $args end]}
      disconnect {pg_disconnect [lindex $args end]}
      execute {pg_exec [lindex $args 0] [lindex $args 1]}
      getrow {pg_result [lindex $args 0] -getTuple [lindex $args 1]}
      clear {pg_result [lindex $args 0] -clear}
      status {pg_result [lindex $args 0] -status}
      rowcount {pg_result [lindex $args 0] -numTuples}
   }
 }

This adds a command pg to Tcl which I like far more than the original one. Instead of saying

 pg_connect "mydb"
 pg_disconnect $myHandle
 pg_result $myResult -clear

I just type

 pg connect "mydb"
 pg disconnect $myHandle
 pg clear $myResult

Please note, that there is no error handling and checking in the command pg and you can easily type in wrong things. It would be best to put this command in it's own namespace and provide for error checking etc. You could even add more funcionality like a loop command for query results like in nstcl


I wrote a tcl interface to PostgreSQL (you can find it here http://sourceforge.net/projects/libtclpq/ ) which supports notify and asynchronous operations on the database (that is, it allows you to cancel pending operations :) CMcC


Category Database