Method Description
PgsqlConnection.connect()
Attach to a Postgres database server.
See Also
PgsqlConnection PgsqlConnection.pconnect Syntax
boolean = PgsqlConnection.connect(connectParams)
Parameters
The 'PgsqlConnection.connect' method takes 1 parameter:
Results
The 'PgsqlConnection.connect' method returns boolean:
Type/Value | Range/Length | Description |
boolean |
  |
true if the connection is made, otherwise throws an exception
|
Remarks
Attach to a Postgres server and open a database
Example The following simple example reads the contents of an arbitrary SQL table called 'TestTable' and outputs
all the columns to the output page.
<rb:script>
var pgconn = new PgsqlConnection;
var pgres;
if (pgconn.connect({dbname:'mydb', user:'me', password:'mydbpwd', host:localhost'})) {
// Connected to the database - read in the contents of the table.
pgres = pgconn.exec('SELECT * FROM TestTable');
}
</rb:script>
<!-- If there are any results then output the table. -->
<rb:if expr="pgres!=null && pgres.success && pgres.numRows>0">
<table>
<rb:repeatfor expr="var r=0;r<pgres.numRows && r<20;r++">
<rb:script>
// Get this row
var row = pgres.fetchRowObject(r);
<rb:script>
<rb:if expr="r==0">
<!-- This is the first row so output row titles -->
<tr>
<rb:repeatfor expr="var ch in row">
<td><?= ch ?></td>
</rb:repeatfor>
</tr>
</rb:if>
<tr>
<rb:repeatfor expr="var ch in row">
<td><?= row[ch] ?></td>
</rb:repeatfor>
</tr>
</rb:repeatfor>
</table>
At the end of a page generation all connections to Postgres openned with this (connect) method are closed. Note
that if the same database is openned very frequently this will be slow. See instead the
pconnect
method for creating persistent database connections. A separate Postgres tutorial explains how to use the PgsqlConnection object in Whitebeam |