连接到Postgresql数据库并执行SQL命令时调用Sqlcmd失败
我已经能够通过OdbcConnection连接到PostgreSQL服务器连接到Postgresql数据库并执行SQL命令时调用Sqlcmd失败,postgresql,powershell,Postgresql,Powershell,我已经能够通过OdbcConnection连接到PostgreSQL服务器 $DBConnectionString = "Driver={PostgreSQL ANSI(x64)};Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;" $DBConn = New-Object System.Data.Odbc.OdbcConnection; $DBConn.ConnectionString = $DBConne
$DBConnectionString = "Driver={PostgreSQL ANSI(x64)};Server=$MyServer;Port=$MyPort;Database=$MyDB;Uid=$MyUid;Pwd=$MyPass;"
$DBConn = New-Object System.Data.Odbc.OdbcConnection;
$DBConn.ConnectionString = $DBConnectionString;
$DBConn.Open();
但是,我希望使用Invoke Sqkcmd命令连接到PostgreSQL。我被告知要始终尝试使用cmdlet及其命令。我试了一下:
$MyPort = '5432'
$MyUid = 'postgres'
$MyPass = 'pass'
$DBSvr = 'server'
$DBase = 'database'
$sqlcmd = 'SELECT email_addr FROM member_subscribers LIMIT 20'
$data = Invoke-Sqlcmd -query $sqlcmd -Database $DBase -ServerInstance $DBSvr$MyPort -Username $MyUid -Password $MyPass
Write-Output $data
然而,我得到了错误
Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and
that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server)
At line:13 char:9
+ $data = Invoke-Sqlcmd -query $sqlcmd -Database $DBase -ServerInstance ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Invoke-Sqlcmd :
At line:13 char:9
+ $data = Invoke-Sqlcmd -query $sqlcmd -Database $DBase -ServerInstance ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException
+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScri
ptCommand
我知道凭证应该是正确的。所以可能格式是错误的?我不确定….Invoke-SQLCmd是否是SQL Server cmdlet。您无法使用它连接到postgres。我猜没有可以连接到postgres的cmdlet?没有本机cmdlet,但我已经编写了一个适用于任何ADO.NET提供程序的“框架”。我还没有为Postgres编写子模块,但它应该非常简单。