2016-05-18 67 views
0

我有幾個SQL腳本可以使用PowerShell針對PostgreSQL實例運行。 我已經安裝了PSQL 9.5以及ODBC驅動程序。我也將它添加到DSN列表無法打開與PostgreSQL的連接

名稱:PostgreSQL35W
驅動程序:在PostgreSQL的Unicode(64)

在PowerShell中我有一個代碼塊

$DBConnectionString = "Driver={PostgreSQL Unicode(x64)};Server=$dbServer;Port=5432;Uid=$dbUser;Pwd=$dbPass;" 
$DBConn = New-Object System.Data.Odbc.OdbcConnection; 
$DBConn.ConnectionString = $DBConnectionString; 
$DBConn.Open(); 
$DBCmd = $DBConn.CreateCommand(); 
$DBCmd.CommandText = $script; 
$DBCmd.ExecuteReader(); 
$DBConn.Close(); 

當試圖連接到PSQL例如,我收到一個沒有說明的例外:

Exception calling "Open" with "0" argument(s): "" 
At C:\Projects\Scripts\Publish\PublishDB.ps1:34 char:5 
+  $DBConn.Open(); 
+  ~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : OdbcException

試圖找到任何有關相關問題 - 沒有運氣。

+1

如果你定義你應該使用[DSN連接字符串(https://www.connectionstrings.com/odbc-dsn/),否則將DSN將DSN毫無意義。如果您想使用「常規」[PostgreSQL ODBC連接字符串](https://www.connectionstrings.com/postgresql-odbc-driver-psqlodbc/):您是否定義了您在其中使用的變量?另外,你似乎沒有指定數據庫。你是否嘗試'PostgreSQL UNICODE'而不是'PostgreSQL Unicode(x64)'作爲驅動程序? –

+0

我做了,它說這樣的驅動程序沒有安裝。 – Refraction

回答

0

正如@Ansgar Wiechers提到的那樣,應該使用DSN名稱來代替驅動程序名稱。 所以連接應該如下:

$DBConnectionString = "DSN=PostgreSQL35WServer=$dbServer;Port=5432;Uid=$dbUser;Pwd=$dbPass;"