You can install the driver by using the Windows installer.
After you receive the license key, add it to your connection strings to connect to the data source.
Google Cloud SQL for PostgreSQL is an enterprise-grade, developer-friendly service that offers powerful AI-enhanced tools, helping developers reduce the time spent on database management. It provides a reliable and advanced platform that combines the convenience of a fully managed cloud service with the flexibility and performance of PostgreSQL.
With full compatibility across major PostgreSQL versions, support for popular extensions, more than 100 configurable database flags, and integration with key Google Cloud services such as Google Kubernetes Engine (GKE), BigQuery, and Cloud Run, Cloud SQL for PostgreSQL is a top choice for developers building cloud-native applications that demand enterprise-level features.
This tutorial demonstrates how to connect C# applications to a PostgreSQL instance hosted on Google Cloud using ADO.NET and dotConnect for PostgreSQL. You'll learn how to set up the connection and perform essential database operations directly from your .NET codebase.
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other modern data access technologies for efficient and reliable data management.
Conforms to the latest ADO.NET standards and recent industry innovations for seamless and consistent integration with .NET applications.
Includes many PostgreSQL-specific features and fully supports all unique data types for accurate and complete data representation.
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
Features native integration with Visual Studio and complete design-time support for accelerated development.
Includes priority support, detailed documentation, and regular updates for continuous improvement.
You can start using dotConnect for PostgreSQL immediately with a 30-day free trial. Choose one of the following installation options:
Let us connect to Cloud SQL for PostgreSQL with the help of the built-in Data Explorer. Click Tools, then select Connect to Database, and choose PostgreSQL as the data source.
By default, Google Cloud for PostgreSQL requires an SSL connection. To enable it, go to the Google Cloud Console > SQL > your PostgreSQL instance > Connections > Security > Certificates, and download the Server CA certificate.
Open Advanced Properties in your application and specify the downloaded certificate file under the SSL configuration before connecting.
Enter your server details and credentials, and click Connect.
Once connected, you can browse tables, execute queries, and manage data directly within the Data Explorer.
Let us establish a connection with Cloud SQL for PostgreSQL and then run a simple SELECT query to pull records from the Actor table. It confirms the connection is working properly and the data is accessible.
The DatabaseConnection.cs class keeps connection details separate for better maintainability.
using Devart.Data.PostgreSql;
public static class DatabaseConnection
{
public static PgSqlConnection CreateConnection()
{
string connectionString = "" +
"Server=127.0.0.1;" +
"User Id=TestUser;" +
"Password=TestPassword;" +
"Database=TestDatabase;" +
"Initial Schema=public;" +
"SSLMode=Allow;" +
"SSL CA Cert=file://C:\\server-ca.pem" +
"License key=**********";
return new PgSqlConnection(connectionString);
}
}
| Property | Description |
|---|---|
| Host or Server | Host name or IP address of the PostgreSQL server |
| User Id | User ID used to authenticate with PostgreSQL |
| Password | Password for the user ID |
| Database | Default database to use after connecting |
| Schema | PostgreSQL schema to use |
| Port | Port on which the PostgreSQL server listens |
| License key | Your license key (only required when you use .NET Standard-compatible assemblies) |
Replace the placeholders in the connection string with your actual PostgreSQL database credentials. If you own a paid license of dotConnect for PostgreSQL, include the license key in the connection strings.
Add the code below to your Program.cs file.
using Devart.Data.PostgreSql;
public sealed class Program
{
public static void Main(string[] args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!\n");
string sql = "SELECT actor_id, first_name, last_name FROM actor ORDER BY actor_id LIMIT 10";
using (PgSqlCommand command = new PgSqlCommand(sql, connection))
using (PgSqlDataReader reader = command.ExecuteReader())
{
Console.WriteLine("+---------+----------------+----------------+");
Console.WriteLine("| ActorId | First Name | Last Name |");
Console.WriteLine("+---------+----------------+----------------+");
while (reader.Read())
{
int actorId = reader.GetInt32(0);
string firstName = reader.GetString(1);
string lastName = reader.GetString(2);
Console.WriteLine($"| {actorId,-7} | {firstName,-14} | {lastName,-14} |");
}
Console.WriteLine("+---------+----------------+----------------+");
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Build and run your application: select Start from the menu or click F5. It displays the data retrieved from the database.
Let's add a new row to the Actor table by executing an INSERT command. See how to prepare and execute an INSERT statement from C# using standard ADO.NET code.
using Devart.Data.PostgreSql;
public sealed class Program
{
public static void Main(string[] args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
string insertSql = "INSERT INTO actor(first_name, last_name) VALUES(@first, @last) RETURNING actor_id";
using (PgSqlCommand insertCmd = new PgSqlCommand(insertSql, connection))
{
insertCmd.Parameters.AddWithValue("@first", "TestFirst");
insertCmd.Parameters.AddWithValue("@last", "TestLast");
object result = insertCmd.ExecuteScalar();
long newActorId = 0;
if (result != null && result != DBNull.Value)
{
newActorId = Convert.ToInt64(result);
}
Console.WriteLine("+---------+----------------+----------------+");
Console.WriteLine("| ActorId | First Name | Last Name |");
Console.WriteLine("+---------+----------------+----------------+");
Console.WriteLine($"| {newActorId,-7} | {"TestFirst",-14} | {"TestLast",-14} |");
Console.WriteLine("+---------+----------------+----------------+");
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
We can see the results in the application:
Next, we want to update an existing record: let us change the actor's first and last name. The following code shows how to build and run an UPDATE command from the application to modify data already stored in the database.
using Devart.Data.PostgreSql;
public sealed class Program
{
public static void Main(string[] args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
long actorId = 202;
string firstName = "James";
string lastName = "Johnson";
string updateSql = "UPDATE actor SET first_name = @first, last_name = @last WHERE actor_id = @id";
using (PgSqlCommand updateCmd = new PgSqlCommand(updateSql, connection))
{
updateCmd.Parameters.AddWithValue("@first", firstName);
updateCmd.Parameters.AddWithValue("@last", lastName);
updateCmd.Parameters.AddWithValue("@id", actorId);
int rowsAffected = updateCmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("+---------+----------------+----------------+");
Console.WriteLine("| ActorId | First Name | Last Name |");
Console.WriteLine("+---------+----------------+----------------+");
Console.WriteLine($"| {actorId,-7} | {firstName,-14} | {lastName,-14} |");
Console.WriteLine("+---------+----------------+----------------+");
}
else
{
Console.WriteLine($"No actor found with actor_id: {actorId}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Our application shows that the operation is successful:
Finally, let us see how to delete the record for that actor we have just added to the database. We run the DELETE statement and remove rows based on a condition in a query.
using Devart.Data.PostgreSql;
public sealed class Program
{
public static void Main(string[] args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
long actorId = 202;
string deleteSql = "DELETE FROM actor WHERE actor_id = @id";
using (PgSqlCommand deleteCmd = new PgSqlCommand(deleteSql, connection))
{
deleteCmd.Parameters.AddWithValue("@id", actorId);
int rowsAffected = deleteCmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine($"+---------+");
Console.WriteLine("| ActorId |");
Console.WriteLine("+---------+");
Console.WriteLine($"| {actorId,-7} |");
Console.WriteLine("+---------+");
Console.WriteLine($"Actor with actor_id {actorId} has been removed.");
}
else
{
Console.WriteLine($"No actor found with actor_id: {actorId}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
The command is executed correctly, and the output in the application confirms that the record has been deleted.
Now that you know the essentials of connecting to and working with databases hosted in Google's Cloud SQL for PostgreSQL from a .NET application, you can connect, query, insert, update, and delete records easily.
With these fundamentals in place, you can proceed to more complex techniques and build applications that take full advantage of PostgreSQL in the cloud, while dotConnect for PostgreSQL ensures a smooth, secure, and efficient connection experience.
I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.