Follow the steps below to write a VBScript in DIAdem that connects to and queries an external PostgreSQL database:
- First, download and install the ODBC driver for Postgres [External] here.
- This driver allows you to connect to local or remote Postgres databases.
- Once installed, launch the ODBC Data Sources application from the Windows Start Menu.
- Select either the User DSN or System DSN tab depending on your needs:
- User DSN makes the PostgreSQL database available for the current user.
- System DSN makes the PostgreSQL database available for all users and services on the machine.
- Click Add... from the right-hand side to select a driver.

- In the pop-up window, scroll and select one of the following drivers:
- PostgreSQL Unicode: if your database was set up with the UTF-8 character set.
- PostgreSQL ANSI: if your database was set up with a LATIN character set.
- Click Finish.

- Configure the following settings:
- Data Source should be a descriptive name.
- Database should be the name of the database you wish to connect to. Set the value as postgres to connect to all databases.
- Server should be the IP address of the server.
- User Name should be the Postgres database username.
- Port should be the port used for the connection.
- Password should be the Postgres database password.
- Click Save to save the configuration settings.

- Click OK to close the ODBC Data Sources app.
- Launch DIAdem.
- From the SCRIPT Panel, select File >> New VBS.
- Save the script.
- Enter the following line to declare variables that will be used in the script. The variables are as follows:
- Conn: used to create an ADODB connection object.
- ConnectionString: used to define the connection parameters to the Postgres database.
- rs: used to hold a set of records from a database table.
- Record: used to copy records from rs into a 2D array.
- objFSO: used to create a File System Object.
- objFile: used to read from and write to a file.
- TableName: used to define the name of the Postgres database table.
- Item: used to iterate through records in the Record array.
Dim Conn, ConnectionString, rs, Record, objFSO, objFile, TableName, Item
- Next, enter the below lines to create a connection to the Postgres database.
- Modify the Server, Port, Database, UID and PWD in ConnectionString for your specific database.
' Create connection
Set Conn = CreateObject("ADODB.Connection")
ConnectionString = "Driver={PostgreSQL UNICODE};Server=localhost;Port=5433;Database=databasename;UID=myusername;PWD=mypassword;"
Conn.Open ConnectionString
- Define the name of the table to query and retrieve the record.
- The below code queries a table called "tablename".result and limits the record set to 10 rows.
'Query the Database table
Set rs = CreateObject("ADODB.recordset")
TableName = Chr(34) & "tablename" & Chr(34) 'Chr(34) ensures that double quotations surround the table name
rs.Open "select * from " & TableName & ".results limit 10", Conn
- Store the records in an array and close the connection using the below lines.
Record = rs.GetRows()
rs.Close
Conn.Close
- [OPTIONAL] Write the results to a file.
- Change the path defined in objFile to suit your requirements.
'Write results to file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\dbData.txt", 8, True)
For Each Item In Record
objFile.WriteLine (Str(Item))
Next