Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

Getting Started With dotConnect for PostgreSQL

Below are some resources to help you maximize your experience with dotConnect for PostgreSQL.

dotConnect for PostgreSQL is a high-performance ADO.NET data provider with ORM support, offering fast and direct access to PostgreSQL databases for application development.

  • EF Core, Dapper, NHibernate, LinqConnect, and other ORMs support
  • SSL, SSH, and proxy servers support
  • Broad compatibility with various .NET platforms and versions
  • Full compliance with ADO.NET
  • Integration with Visual Studio

Code examples

using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    string connectionString = "" +
    "Host=127.0.0.1;" +
    "Port=5432;" +
    "User Id=TestUser;" +
    "Password=TestPassword;" +
    "Database=sakila;" +
    "Schema=public;" +
    "LicenseKey=**********";

    using (PgSqlConnection connection = new PgSqlConnection(connectionString))
    {
      try
      {
        connection.Open();
        Console.WriteLine("Connection to PostgreSQL successful!");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"PostgreSQL Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    var builder = new PgSqlConnectionStringBuilder
    {
      Host = "127.0.0.1",
      Port = 5432,
      UserId = "TestUser",
      Password = "TestPassword",
      Database = "sakila",
      Schema = "public", 
      LicenseKey = "**********"
    };

    using (PgSqlConnection connection = new PgSqlConnection(builder.ConnectionString))
    {
      // Set SSL options
      connection.SslOptions.CACert = @"\CACert.crt";
      connection.SslOptions.Cert = @"\Cert.crt";
      connection.SslOptions.Key = @"\Key.key";
      connection.SslOptions.SslMode = SslMode.Require;

      try
      {
        connection.Open();
        Console.WriteLine("SSL connection to PostgreSQL successful!");
      }
      catch (PgSqlException ex)
      {
        Console.WriteLine($"PostgreSQL SSL Error: {ex.Message}");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"General Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    var builder = new PgSqlConnectionStringBuilder
    {
      Host = "127.0.0.1",
      Port = 5432,
      UserId = "TestUser",
      Password = "TestPassword",
      Database = "sakila",
      Schema = "public",
      LicenseKey = "**********"
    };

    using (PgSqlConnection connection = new PgSqlConnection(builder.ConnectionString))
    {
      // Configure SSH tunnel
      connection.SshOptions.Host = "sshServer";
      connection.SshOptions.User = "sshUser";
      connection.SshOptions.Password = "sshPassword";
      connection.SshOptions.AuthenticationType = SshAuthenticationType.Password;

      try
      {
        connection.Open();
        Console.WriteLine("SSH connection to PostgreSQL successful!");
      }
      catch (PgSqlException ex)
      {
        Console.WriteLine($"PostgreSQL SSH Error: {ex.Message}");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"General Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    var builder = new PgSqlConnectionStringBuilder
    {
      Host = "127.0.0.1",
      Port = 5432,
      UserId = "TestUser",
      Password = "TestPassword",
      Database = "sakila",
      Schema = "public",
      LicenseKey = "**********"
    };

    using (PgSqlConnection connection = new PgSqlConnection(builder.ConnectionString))
    {
      // Configure proxy settings
      connection.ProxyOptions.Host = "10.0.0.1";
      connection.ProxyOptions.Port = 808;
      connection.ProxyOptions.User = "ProxyUser";
      connection.ProxyOptions.Password = "ProxyPassword";

      try
      {
        connection.Open();
        Console.WriteLine("Proxy connection to PostgreSQL successful!");
      }
      catch (PgSqlException ex)
      {
        Console.WriteLine($"PostgreSQL Proxy Error: {ex.Message}");
      }
      catch (Exception ex)
      {
        Console.WriteLine($"General Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    string connectionString =
    "Host=127.0.0.1;" +
    "Port=5432;" +
    "UserId=TestUser;" +
    "Password=TestPassword;" +
    "Database=sakila;" +
    "Schema=public;" +
    "LicenseKey=**********";

    using (PgSqlConnection connection = new PgSqlConnection(connectionString))
    {
      try
      {
        connection.Open();
        Console.WriteLine("PostgreSQL connection successful!");

        // Define a query to select data from the actor table
        string query = "SELECT actor_id, first_name, last_name FROM actor";

        // Create a command object
        using (PgSqlCommand command = new PgSqlCommand(query, connection))
        {
          // Execute the query and get a data reader
          using (PgSqlDataReader reader = command.ExecuteReader())
          {
            // Loop through the result set
            while (reader.Read())
            {
              // Access the data using column names
              int actorId = reader.GetInt32(reader.GetOrdinal("actor_id"));
              string firstName = reader.GetString(reader.GetOrdinal("first_name"));
              string lastName = reader.GetString(reader.GetOrdinal("last_name"));

              // Print the data to the console
              Console.WriteLine($"Actor ID: {actorId}, Name: {firstName} {lastName}");
            }
          }
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using System;
using Microsoft.EntityFrameworkCore;
using Devart.Data.PostgreSql.EFCore;

public class Actor
{
  public int ActorId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

public class SakilaContext : DbContext
{
  public DbSet<Actor> Actors { get; set; }

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
    optionsBuilder.UsePostgreSql(
      "Host=127.0.0.1;Port=5432;UserId=TestUser;Password=TestPassword;Database=sakila;Schema=public;LicenseKey=**********",
      PostgreSqlServerVersion.LatestSupportedServerVersion
    );
  }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Actor>().ToTable("actor"); // ensure correct table mapping
    modelBuilder.Entity<Actor>().Property(a => a.ActorId).HasColumnName("actor_id");
    modelBuilder.Entity<Actor>().Property(a => a.FirstName).HasColumnName("first_name");
    modelBuilder.Entity<Actor>().Property(a => a.LastName).HasColumnName("last_name");
  }
}

class Program
{
  static void Main()
  {
    using (var context = new SakilaContext())
    {
      try
      {
        // Ensure the database is created (optional for read-only usage)
        context.Database.EnsureCreated();
        Console.WriteLine("Connected to PostgreSQL database successfully!");

        // Query data from the Actors table
        var actors = context.Actors;
        foreach (var actor in actors)
        {
          Console.WriteLine($"Actor ID: {actor.ActorId}, Name: {actor.FirstName} {actor.LastName}");
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql.EFCore
using System;
using System.Data;
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    string connectionString =
    "Host=127.0.0.1;" +
    "Port=5432;" +
    "UserId=TestUser;" +
    "Password=TestPassword;" +
    "Database=sakila;" +
    "Schema=public;" +
    "LicenseKey=**********";

    using (PgSqlConnection connection = new PgSqlConnection(connectionString))
    {
      try
      {
        connection.Open();
        Console.WriteLine("PostgreSQL connection successful!");

        // Define a query to select data from the actor table
        string query = "SELECT actor_id, first_name, last_name FROM actor";

        // Create a command object
        using (PgSqlCommand command = new PgSqlCommand(query, connection))
        {
          // Create a data adapter
          using (PgSqlDataAdapter adapter = new PgSqlDataAdapter(command))
          {
            // Create a DataTable to hold the data
            DataTable dataTable = new DataTable();

            // Fill the DataTable with data from the database
            adapter.Fill(dataTable);

            // Loop through the DataTable and print the data
            foreach (DataRow row in dataTable.Rows)
            {
              int actorId = (int)row["actor_id"];
              string firstName = row["first_name"].ToString();
              string lastName = row["last_name"].ToString();

              Console.WriteLine($"Actor ID: {actorId}, Name: {firstName} {lastName}");
            }
          }
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
      string connectionString =
        "Host=127.0.0.1;" +
        "Port=5432;" +
        "UserId=TestUser;" +
        "Password=TestPassword;" +
        "Database=sakila;" +
        "Schema=public;" +
        "LicenseKey=**********";

      using (PgSqlConnection connection = new PgSqlConnection(connectionString))
      {
        try
        {
          connection.Open();
          Console.WriteLine("PostgreSQL connection successful!");

          // Start a transaction
          using (PgSqlTransaction transaction = connection.BeginTransaction())
          {
            try
            {
              // Create a command to insert a new actor
              string insertQuery = "INSERT INTO actor (first_name, last_name) VALUES ('John', 'Doe')";
              using (PgSqlCommand insertCommand = new PgSqlCommand(insertQuery, connection, transaction))
              {
                insertCommand.ExecuteNonQuery();
                Console.WriteLine("Insert command executed.");
              }

              // Create a command to update the inserted actor
              string updateQuery = "UPDATE actor SET last_name = 'Smith' WHERE first_name = 'John' AND last_name = 'Doe'";
              using (PgSqlCommand updateCommand = new PgSqlCommand(updateQuery, connection, transaction))
              {
                updateCommand.ExecuteNonQuery();
                Console.WriteLine("Update command executed.");
              }

              // Commit the transaction
              transaction.Commit();
              Console.WriteLine("Transaction committed successfully.");
            }
            catch (Exception ex)
            {
              // Roll back the transaction if any command fails
              transaction.Rollback();
              Console.WriteLine($"Transaction rolled back due to an error: {ex.Message}");
            }
          }
        }
        catch (Exception ex)
        {
          Console.WriteLine($"Connection error: {ex.Message}");
        }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql
using Devart.Data.PostgreSql;

class Program
{
  static void Main()
  {
    string connectionString =
    "Host=127.0.0.1;" +
    "Port=5432;" +
    "UserId=TestUser;" +
    "Password=TestPassword;" +
    "Database=sakila;" +
    "Schema=public;" +
    "LicenseKey=**********";

    using (PgSqlConnection connection = new PgSqlConnection(connectionString))
    {
      try
      {
        connection.Open();
        Console.WriteLine("PostgreSQL connection successful!");

        // Attempt to execute a query that may cause an exception
        string query = "SELECT * FROM non_existent_table";
        using (PgSqlCommand command = new PgSqlCommand(query, connection))
        {
          using (PgSqlDataReader reader = command.ExecuteReader())
          {
            while (reader.Read())
            {
              // This will not be reached if the table does not exist
              Console.WriteLine(reader["column_name"]);
            }
          }
        }
      }
      catch (PgSqlException ex)
      {
        // Handle PostgreSQL-specific exception
        Console.WriteLine($"PostgreSQL Error Message: {ex.Message}");
        Console.WriteLine($"SQL State: {ex.SqlState}");
        Console.WriteLine($"Error Code: {ex.ErrorCode}");
        Console.WriteLine($"Stack Trace: {ex.StackTrace}");
      }
      catch (Exception ex)
      {
        // Handle any general .NET exception
        Console.WriteLine($"General Error: {ex.Message}");
      }
    }
  }
}
PM> Install-Package Devart.Data.PostgreSql

Demo videos

How-to articles

.NET PostgreSQL Connection Strings
Learn how to set up connection strings properly when working with dotConnect for PostgreSQL in .NET applications.
Connect to PostgreSQL With EF Core
Find out how to connect a PostgreSQL database to your application using EF Core and ADO.NET.
Connect to PostgreSQL With Dapper
Learn how to connect C# to PostgreSQL using Dapper for fast and efficient data access and management.
Connect to PostgreSQL in .NET
Explore how to establish a connection between .NET applications and PostgreSQL databases using ADO.NET.
Connect to PostgreSQL in Blazor
Learn how to integrate PostgreSQL with .NET Blazor applications using EF Core and ADO.NET.
Connect to PostgreSQL in MAUI
See how to build .NET MAUI applications and perform CRUD operations with PostgreSQL using ADO.NET.
Connect to PostgreSQL with NHibernate
Discover how to connect C# to PostgreSQL using NHibernate seamlessly.

Documentation

Licensing

Licensing

A detailed technical reference on embedding license information - a required resource for applications developed with dotConnect for PostgreSQL.
DataTable Features

DataTable features

A guide to using the PgSqlDataTable component and its features, like server connections, command objects, data storage, and data access.
Parameters

Parameters

An overview of SQL query parameters and their synchronization, along with practical insights into using parameters in stored procedures to ensure correct performance.
Specific Features

Specific features

An explanation of support for PostgreSQL-specific technologies, including secure connections, load balancing, embedded servers, and more.

dotConnect Universal

Get universal access to data from a variety of sources, including SQL Server, Oracle, PostgreSQL, MySQL, SQLite, DB2, InterBase, Microsoft Access, and Firebird. Other servers can be accessed through their ADO.NET, OLE DB and ODBC providers.

FAQ

What is the trial period for dotConnect for PostgreSQL?
dotConnect for PostgreSQL offers a fully functional 30-day trial. During this period, you can use and evaluate all features without limitations. After the trial ends, you can purchase a license to keep using the product.
Why do I need to install the product if NuGet is available?

dotConnect for PostgreSQL provides two sets of assemblies, and the choice depends on your project type:

  • For .NET Framework projects: Use the Devart assemblies included with the product installer.
  • For .NET (.NET Core/.NET 5+): Use the assemblies available via NuGet packages.
Where should I apply my License Key?

The method for applying your license depends on your project type:

  • For .NET (.NET Core/.NET 5+) projects, apply the License Key at runtime using the License Key connection string parameter.
  • For .NET Framework projects, no license key is needed. Simply download and install the licensed version of dotConnect from your Devart Customer Portal.
Do end users of my application need a Devart license, or do I have to pay additional fees for deploying my software?

No, end users do not need a Devart license, and there are no additional deployment fees, as long as you hold a valid Devart license.

According to the EULA, you're allowed to redistribute the following runtime assemblies with your application:

  • Devart.Data.PostgreSql.dll
  • Devart.Data.dll

You can include them in your app's folder (such as Bin for web apps) or register them in the GAC. Just make sure they are only used by your solution.

There's no need for a full installation on the target machine, you can also use the "Minimal installation" option provided by the setup.

No other components of dotConnect may be distributed.

What editions of dotConnect for PostgreSQL are available, and what's the difference?

There are five editions:

  • Express - Free version with basic connectivity.
  • Standard - Full ADO.NET provider with design-time support.
  • Professional - Adds ORM tools like Entity Framework and LinqConnect.
  • Developer - Includes all Professional features.
  • Mobile - Provides Standard features for the .NET Compact Framework.
How can I upgrade dotConnect to a new version?

To upgrade dotConnect to a new version, you need to reinstall it:

  • Open Settings > Apps > Installed apps in Windows.
  • Uninstall the current and any older versions of dotConnect, including Entity Developer and LinqConnect, if installed.
  • Visit the Customer Portal and download the latest licensed version.
  • Install the downloaded version and receive the latest features and improvements.
How can I completely remove all previous versions of a product?
  1. Go to Settings > Apps > Installed apps and uninstall:
    • All dotConnect providers
    • Entity Developer
    • LinqConnect
  2. Manually delete any leftover files from the following locations (if they exist):

    GAC folders:

    • C:\Windows\assembly\GAC_MSIL
    • C:\Windows\Microsoft.NET\assembly\GAC_MSIL

    Program files and extensions:

    • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Devart
    • C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Devart
    • C:\Program Files (x86)\Devart
    • C:\Program Files\Devart
    • C:\Program Files (x86)\Common Files\Devart
    • C:\Program Files\Common Files\Devart

    Program data folders:

    • C:\ProgramData\Devart\dotConnect
    • C:\ProgramData\Devart\Entity Developer
    • C:\ProgramData\Devart\EntityDeveloper
    Removing these ensures a clean system and prevents conflicts during future installations.
Wield the full firepower of dotConnect for PostgreSQL
Go with the advanced edition of dotConnect for PostgreSQL and stay at the top of your game from day one!