Introduction
PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. PostgreSQL is a powerful, open-source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
We can download and install the official certified PostgreSQL database from EnterpriseDB (EDB) site.
You can select pgAdmin 4 for administering the database.
Please provide a password for the database superuser.
Choose the default port and locale and start installing the database.
After few minutes, PostgreSQL will be installed on your machine.
We can open pgAdmin 4 tool now.
Please set a master password for pgAdmin. We must give this password, later we open the pgAdmin.
We can see a default server PostgreSQL 12. You must give your superuser password to open this server.
By default, there is one login role “postgres” and some group roles available. We can create one more group role.
You can set a password to your group role in the “Definition” tab.
You must set at least the “Can login” option to “Yes” inside the “Privileges” tab. Otherwise, you can’t do database operations with this group role.
We can create a new database now. I have chosen the owner name as “sarath” which was created recently.
In Postgres, each table comes under schemas. By default, there is a “public” schema for each database.
We can create a new table inside the public schema. You can either create a table using the graphical feature in pgAdmin or by SQL scripts.
We can choose the “Query Tool” from the “Tools” menu to enter SQL scripts and execute.
Create a table “patients” under the “public” schema. We set the owner of the table to “sarath”
- CREATE TABLE public.patients
- (
- id character varying(50) NOT NULL,
- name character varying(200) NOT NULL,
- address character varying(500),
- city character varying(100),
- age numeric NOT NULL,
- gender character varying(10),
- CONSTRAINT patient_pkey PRIMARY KEY (id)
- );
-
- ALTER TABLE public.patients
- OWNER TO sarath;
We can use GET request to retrieve the patient record.
We can modify the existing patient record by PUT request.
You can perform the DELETE request also in the same way.
We have successfully executed all CRUD operations in PostgreSQL along with ASP.NET Core Web API using the entity framework.
Conclusion
In this post, we have seen how to install PostgreSQL on a windows machine and we have created a simple Web API application with ASP.NET Core 3.1 framework. We have created a DataContext class and DataAccessProvider class to access the PostgreSQL database. We have used Entity Framework Core for data mapping. We have successfully checked all the CRUD operations using the Postman tool.