Pipe CockroachDB Data to CSV in PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

CockroachDB Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to CockroachDB. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.



Use standard PowerShell cmdlets to access CockroachDB tables.

The CData Cmdlets Module for CockroachDB is a standard PowerShell module offering straightforward integration with CockroachDB. Below, you will find examples of using our CockroachDB Cmdlets with native PowerShell cmdlets.

Creating a Connection to Your CockroachDB Data

Set the following to connect to CockroachDB.

  • Server: The host name or IP address of the server.
  • Port: The port number of the CockroachDB server. If not specified, the default port is 26257.
  • Database: The name of the Cockroach database. If not specified, you connect to the user's default database.
  • User: The Cockroach DB user account used to authenticate.
  • Password: The password used to authenticate the user.

$conn = Connect-CockroachDB  -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"

Selecting Data

Follow the steps below to retrieve data from the Orders table and pipe the result into to a CSV file:

Select-CockroachDB -Connection $conn -Table Orders | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation

You will notice that we piped the results from Select-CockroachDB into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-CockroachDB -Connection $conn -Table Orders -Where "ShipCountry = USA" | Remove-CockroachDB

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into CockroachDB, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyOrdersUpdates.csv | %{
  $record = Select-CockroachDB -Connection $CockroachDB -Table Orders -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-CockroachDB -Connection $cockroachdb -Table Orders -Columns ("ShipName","ShipCity") -Values ($_.ShipName, $_.ShipCity) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-CockroachDB -Connection $cockroachdb -Table Orders -Columns ("ShipName","ShipCity") -Values ($_.ShipName, $_.ShipCity)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!