title: How to query PostgreSQL from VBA
body: <p>How can I query PostreSQL in VBA? I have tried the following which I found online but doesn't seem to work.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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."</p>
  
  <p>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.</p>