Connect .NET Core With PostgreSQL On A Docker Container

In this post, we will learn to quickly setup a .NET core app to connect with PostgreSQL database on a docker container environment. We will setup PostgreSQL with Docker Compose to have persistent data volume. Then we will connect to that database with the help of Entity Framework on a .NET core web API.

Setup PostgreSQL With Docker Compose

We want our data to be persistent even after the PostgreSQL docker container is closed and re-started. For this we want to use use local volumes during the docker compose.

Start by creating a folder named “docker” inside your project root. Inside this folder, we will add a file called “docker-compose.yml“.
Our docker-compose.yml file will look something like this (for windows platform):

Now to run or stop the container, we will have two separate script files: start_db.sh and stop_db.sh.

Create another folder and call it “scripts“.

First create start_db.sh.

Then create stop_db.sh.

Now we are ready to create our PostgreSQL docker container.

Running PostgreSQL Container

Open docker terminal.

Docker Terminal
Docker Terminal

From your project root folder, use the command below to create an image and run the container for PostgreSQL.

Verify if the container was created and is running by using this command:

Connecting To PostgreSQL Docker Container With pgAdmin

To check to see if you can access the PostgreSQL container, you can use the pgAdmin client tool. Use the correct values for host, port, username and password that you set earlier for making the connection.

Become A Flutter Expert With This Course

For host address, you can use “localhost“. If you are on Windows and are using docker with VirtualBox you might need to use the actual IP for your docker machine.

Connecting To PostgreSQL Docker Container With .NET Core App

Now that we have a working PostgreSQL running on a docker container, we move onto the second part. We will connect to this container with .NET core app. We will use Entity Framework code first approach to create a table on the PostgreSQL server.

In your .NET app, create an entity class called Login.cs

This is just a basic class for now. Now we need to create a database context object.

Create a class called “MyDbContext.cs” which will look like this:

Here, we created a sample database context for table Logins. The database connection string is hard-coded for demo.

Running Entity Framework Migrations On PostgreSQL

We will use the Npgsql EF Core provider library to make use of our database context. Add a reference to the library in your app’s .csproj file:

Finally adjust the ConfigureServices method of the Startup.cs file so that it looks like this:

Now you can run migrations and update database to create new tables.

You can learn more about running migrations on .NET core app with Entity Framework from the links below:

Entity Framework Code First Approach With .NET Core

Adding Migrations On Entity Framework .NET Core

Conclusion

In this post we successfully created a PostgreSQL server on docker container. We set it up so that the data volumes stayed persistently. Then we connected to this container with a .NET core app and ran Entity Framework code first migrations on it.

About the author