Amazon RDS for PostgreSQL is a fully managed relational database service provided by Amazon Web Services (AWS). It takes care of many complex and time-consuming administrative tasks, making it easier to deploy and manage PostgreSQL databases, even for users without deep database administration experience. With its strong scalability, high availability, and reliable performance, Amazon RDS is a popular choice among PostgreSQL users who want to combine PostgreSQL's flexibility with the advantages of a fully managed cloud platform.
This guide will show you how to connect a C# application to a PostgreSQL database hosted on Amazon RDS. You'll learn how to configure the connection, access data, and perform basic data operations from a .NET environment. To streamline the process and avoid common connectivity issues, we'll also use dotConnect for PostgreSQL. Let's begin.
Why dotConnect for PostgreSQL?
Advanced ORM support
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.
Full ADO.NET compliance
Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.
PostgreSQL-specific data types
Offers many PostgreSQL-specific features and fully supports all unique data types for accurate and complete data representation.
Secure connection options
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
IDE integration
Features native integration with Visual Studio and complete design-time support for accelerated development.
Priority support & frequent updates
Includes priority support, detailed documentation, and regular updates for continuous improvement.
Download and activate dotConnect for PostgreSQL
You can start using dotConnect for PostgreSQL immediately with a 30-day free trial. Choose one of the following installation options:
Connect to Amazon RDS for PostgreSQL with Data Explorer
The first option is connecting to Amazon RDS for PostgreSQL via the built-in Data Explorer in Visual Studio. Here's how to do this:
1. Click Tools, then select Connect to Database, and choose PostgreSQL as the data source.
By default, AWS RDS for PostgreSQL requires an SSL connection. The steps below explain how to complete the connection process:
2. Open Advanced Properties in the connection settings in your application and enable the SSLMode for connecting.
3. Enter your server details and credentials, and click Connect.
4. Once connected, you can browse tables, execute queries, and manage data directly within the Data Explorer.
Connect and retrieve data from the database
Let's establish a connection with Amazon RDS for PostgreSQL and then run a simple SELECT query to pull records from the Actor table. The query confirms that the connection is established correctly and the data is accessible.
Create the DatabaseConnection.cs class (connection strings)
The DatabaseConnection.cs class keeps connection details separate for better maintainability.
Make sure to replace the placeholders in the code with your actual PostgreSQL database credentials. If you have the paid license of dotConnect for PostgreSQL, include the license key in the code.
Connection properties table
Property
Meaning
Host
Specifies the hostname or IP address of the PostgreSQL server
Port
States the port number on which the PostgreSQL server is listening
UserId
Indicates the user ID used to authenticate with PostgreSQL
Password
Defines the password for the user ID
Database
Specifies the default database to use after connecting
Schema
The PostgreSQL schema to use
License key
Specify your license key in this parameter; this is required only when using assemblies compatible with .NET Standard
using Devart.Data.PostgreSql;
publicsealedclassProgram
{
publicstaticvoidMain(
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
);
}
}
}
Here's the results in the application:
Update rows in Amazon RDS for PostgreSQL
Next, let's update an existing record. First, you'll change the actor's first and last name. This process demonstrates how you can change data directly from your application.
using Devart.Data.PostgreSql;
publicsealedclassProgram
{
publicstaticvoidMain(
string[]
args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
long actorId =
201;
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
);
}
}
}
The output in the application proves that the operation is successful:
Delete rows from Amazon RDS for PostgreSQL
Finally, here's how you can remove the record for the actor you just added to the database. This section shows how to delete data from the PostgreSQL table based on a specific condition.
using Devart.Data.PostgreSql;
publicsealedclassProgram
{
publicstaticvoidMain(
string[]
args)
{
try
{
using (PgSqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
long actorId =
201;
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 successful. We can see that the record is deleted.
Conclusion
Now you can connect to Amazon RDS for PostgreSQL, access your data in PostgreSQL databases, and manipulate that data as necessary. This tutorial explained and illustrated the most fundamental data operations: reading, inserting, updating, and deleting data. With this knowledge, you can move further and build robust and scalable data-driven applications that flawlessly integrate with the AWS infrastructure.
FAQ
How do you install and activate dotConnect for PostgreSQL in a .NET project?
Install dotConnect for PostgreSQL via the EXE installer or by adding the Devart.Data.PostgreSQL NuGet package to your project, then obtain your personal activation key from your Devart Customer Portal and include it in the connection string via the License key parameter for a working connection.
How do you create a connection to PostgreSQL using dotConnect in C#?
Define a connection string that includes the host, user ID, password, database, and License key value. Then create a PgSqlConnection instance with this connection string and call the Open() method inside a try-catch block to test the connection and properly handle any potential errors.
How do you enable SSL/TLS for secure PostgreSQL connections with dotConnect?
Add SslMode=Require and specify the CACert, Cert, and Key file paths in the connection string. Then open the PgSqlConnection connection to establish an encrypted SSL/TLS connection.
Can you connect to PostgreSQL using Entity Framework Core and dotConnect?
Yes, you can either use Entity Developer to visually create an EF Core model from the database or run Scaffold-DbContext with a dotConnect connection string (including your License key) to generate the DbContext and entity classes.
Is it possible to connect to PostgreSQL using Visual Studio Server Explorer with dotConnect?
Yes, it is possible. All you need to do is navigate to Visual Studio Server Explorer, add a new Data Connection, choose dotConnect for PostgreSQL as the data provider, enter your credentials, test the connection, and then browse and manage PostgreSQL data directly in Visual Studio.
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.