The examples in this section show a procedure to return a list of
the databases known to the server (which are stored in the
pg_database
table).
Note that the connect command does not specify a username or password. This would usually fail, unless the credentials are provided through some other means such as the environment variables.
The first example uses the pg_exec function.
Example 5.3. Connect and get list of databases - pg_exec
proc getDBs { {host "localhost"} {port "5432"} } { set conn [pg_connect -conninfo "host=$host port=$port dbname=template1"] set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname"] set datnames {} set ntups [pg_result $res -numTuples] for {set i 0} {$i < $ntups} {incr i} { lappend datnames [lindex [pg_result $res -getTuple $i] 0] } pg_result $res -clear pg_disconnect $conn return $datnames }
Here is the same procedure, using pg_select to iterate over the returned rows.
Example 5.4. Connect and get list of databases - pg_select
proc getDBs { {host "localhost"} {port "5432"} } { set conn [pg_connect -conninfo "host=$host port=$port dbname=template1"] set datnames {} pg_select $conn "SELECT datname FROM pg_database ORDER BY datname" data { lappend datnames $data(datname) } pg_disconnect $conn return $datnames }
This version of the manual was produced for the
Pgtcl-ng Sourceforge project web service site, which requires the logo on each
page.
To download a logo-free copy of the manual, see the
Pgtcl-ng project
downloads area.