0

How can I query PostreSQL in VBA? I have tried the following which I found online but doesn't seem to work.

Sub query()

'Create Connection
Set conn = CreateObject("ADODB.Connection")
strCnx = "Driver={PostgreSQL UNICODE};Server=localhost;Database=databasename;uid=username;pwd=password123;"
conn.Open strCnx

'Query the Database
Set rs = CreateObject("ADODB.recordset")
rs.Open "select * from dm.acct_dim limit 10", conn

Record = rs.GetRows()
rs.Close
conn.Close

'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 (Item)
Next

objFile.Close

End Sub

When I try this i get an error saying "could not connect to the server; No Connection could be made because the target machine actively refused it."

When I connect with pgadmin i use host, port, database, username and password. So I'm guessing i need those same things in the connection string but not exactly sure how that should be setup. I'm a rookie here. thanks for any help.

Community
  • 1
  • 1
Robert Petty
  • 197
  • 1
  • 3
  • 17

1 Answers1

0

Looks like it was correct as it was. My problem was that I had wrong server. My bad. Thanks for trying to help!

Robert Petty
  • 197
  • 1
  • 3
  • 17