Ready to get started?

Download a free trial of the PostgreSQL Data Provider to get started:

 Download Now

Learn more:

PostgreSQL Icon PostgreSQL ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with PostgreSQL-compatible database engines.

Automate PostgreSQL Integration Tasks from PowerShell



Are you in search of a quick and easy way to access PostgreSQL data from PowerShell? This article demonstrates how to utilize the PostgreSQL Cmdlets for tasks like connecting to PostgreSQL data, automating operations, downloading data, and more.

The CData Cmdlets for PostgreSQL are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to PostgreSQL.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to PostgreSQL, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete PostgreSQL data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for PostgreSQL. To access PostgreSQL data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for PostgreSQL.

Once you have acquired the necessary connection properties, accessing PostgreSQL data in PowerShell can be enabled in three steps.

To connect to PostgreSQL, set the Server, Port (the default port is 5432), and Database connection properties and set the User and Password you wish to use to authenticate to the server. If the Database property is not specified, the data provider connects to the user's default database.

PowerShell

  1. Install the module:

    Install-Module PostgreSQLCmdlets
  2. Connect:

    $postgresql = Connect-PostgreSQL -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
  3. Search for and retrieve data:

    $shipcountry = "USA" $orders = Select-PostgreSQL -Connection $postgresql -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders

    You can also use the Invoke-PostgreSQL cmdlet to execute SQL commands:

    $orders = Invoke-PostgreSQL -Connection $postgresql -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for PostgreSQL\lib\System.Data.CData.PostgreSQL.dll")
  2. Connect to PostgreSQL:

    $conn= New-Object System.Data.CData.PostgreSQL.PostgreSQLConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5432;") $conn.Open()
  3. Instantiate the PostgreSQLDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT ShipName, ShipCity from Orders" $da= New-Object System.Data.CData.PostgreSQL.PostgreSQLDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.shipname $_.shipcity }

Update PostgreSQL Data

PowerShell

Update-PostgreSQL -Connection $PostgreSQL -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.PostgreSQL.PostgreSQLCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.PostgreSQL.PostgreSQLParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert PostgreSQL Data

PowerShell

Add-PostgreSQL -Connection $PostgreSQL -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")

ADO.NET

$cmd = New-Object System.Data.CData.PostgreSQL.PostgreSQLCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.PostgreSQL.PostgreSQLParameter("@myShipCountry","USA"))) $cmd.ExecuteNonQuery()

Delete PostgreSQL Data

PowerShell

Remove-PostgreSQL -Connection $PostgreSQL -Table "Orders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.PostgreSQL.PostgreSQLCommand("DELETE FROM Orders WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.PostgreSQL.PostgreSQLParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()