I have been able to connect to the PostgreSQL Server with OdbcConnection.
$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();
However I wish to connect to the PostgreSQL with command Invoke-Sqkcmd. I was told to always try to use the cmdlets and their commands. I tried it with:
$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
However, I get the error.
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
I know the credentials should be correct. So maybe the formatting is just wrong? I am not sure....