Automate PostgreSQL Integration Tasks from PowerShell
The CData ADO.NET Provider for PostgreSQL is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to PostgreSQL.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for PostgreSQL; this tutorial shows how to use the Provider to create, retrieve, update, and delete PostgreSQL data.
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.
SSH Connectivity for PostgreSQL
You can use SSH (Secure Shell) to authenticate with PostgreSQL, whether the instance is hosted on-premises or in supported cloud environments. SSH authentication ensures that access is encrypted (as compared to direct network connections).
SSH Connections to PostgreSQL in Password Auth Mode
To connect to PostgreSQL via SSH in Password Auth mode, set the following connection properties:
- User: PostgreSQL User name
- Password: PostgreSQL Password
- Database: PostgreSQL database name
- Server: PostgreSQL Server name
- Port: PostgreSQL port number like 3306
- UserSSH: "true"
- SSHAuthMode: "Password"
- SSHPort: SSH Port number
- SSHServer: SSH Server name
- SSHUser: SSH User name
- SSHPassword: SSH Password
SSH Connections to PostgreSQL in Public Key Auth Mode
To connect to PostgreSQL via SSH in Password Auth mode, set the following connection properties:
- User: PostgreSQL User name
- Password: PostgreSQL Password
- Database: PostgreSQL database name
- Server: PostgreSQL Server name
- Port: PostgreSQL port number like 3306
- UserSSH: "true"
- SSHAuthMode: "Public_Key"
- SSHPort: SSH Port number
- SSHServer: SSH Server name
- SSHUser: SSH User name
- SSHClientCret: the path for the public key certificate file
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for PostgreSQL\lib\System.Data.CData.PostgreSQL.dll") -
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() -
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
$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
$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
$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()
CodeProject