Prisma PostgreSQL REST API Installation: Step-by-Step Guide
📌 Meta Description: How to set up REST API using TypeScript, Prisma and PostgreSQL, what steps to follow with Express—learn it all.
🯠What Will You Learn in This Guide?
- How to start a TypeScript-based Express project.
- Installing PostgreSQL with Docker and connecting it with Prisma ORM.
- Defining a data model and creating tables.
- Implementing REST API routes with GET, POST, PUT, DELETE.
- Establishing a production-ready simple API architecture independent of unit tests.
🧩 Technical Summary
This guide talks about creating a REST API for a blog application with the TypeScript + Express + Prisma + PostgreSQL stack. Purpose: to simplify database queries with ORM (Prisma). Steps: project setup → Prisma + PostgreSQL configuration → data model definition → writing CRUD routes.
Step 1ï¸âƒ£ — Prepare Your TypeScript Project
Create a new project folder and enter it:
mkdir blog-api
cd blog-api
This command creates the project folder.
Create the project definition file with npm init -y.
Install development dependencies:
npm install typescript ts-node @types/node @types/express --save-dev
This command prepares TypeScript and the execution environment.
Create or set the tsconfig.json file as follows:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
This code sets TypeScript compiler options.
npm install express @prisma/client komutuyla gereken kütüphaneleri yükleyin.
This step adds the Express server and Prisma client to your project.
Step 2ï¸âƒ£ — PostgreSQL + Prisma Installation
Install the Prisma CLI with the command npm install prisma --save-dev.
This is required before you can run Prisma commands.
Create a file named docker-compose.yml and add the following content:
version: '3.8'
services:
postgres:
image: postgres:14
restart: always
environment:
- POSTGRES_USER=rbuser
- POSTGRES_PASSWORD=R@b1suPass
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres_data:
This structure stands up the PostgreSQL database with Docker.
To run the database:
docker-compose up -d
This command activates the service in the background.
Run the command npx prisma init. This command creates the prisma/schema.prisma file and the .env file.
In the .env, update the DATABASE_URL value as follows:
DATABASE_URL="postgresql://rbuser:R@b1suPass@localhost:5432/blogdb?schema=public"
This line contains database connection information.
Step 3ï¸âƒ£ — Data Model Definition and Creating Tables
Add the following models to the prisma/schema.prisma file:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
This code defines user and post models.
Run in terminal:
npx prisma migrate dev --name init
This command applies the tables to the database and creates Prisma Client.
Install Prisma Client:
npm install @prisma/client
This step makes the database ready for queries.
Step 4ï¸âƒ£ — Review Simple Prisma Queries
Create the src/index.ts file and add the following code:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
const newUser = await prisma.user.create({
data: {
name: "Alice",
email: "alice@ornek.com",
posts: {
create: { title: "Hello World" }
}
},
include: { posts: true }
});
console.log("Yeni kullanıcı:", newUser);
const allUsers = await prisma.user.findMany({
include: { posts: true }
});
console.log("Tüm kullanıcılar:", allUsers);
}
main()
.catch(e => console.error(e))
.finally(async () => await prisma.$disconnect());
This code creates users and pulls all users from the database.
Run:
npx ts-node src/index.ts
This command runs the script file and displays the results in the terminal.
Step 5ï¸âƒ£ — Implement Your First REST API Route
Update src/index.ts as follows:
import express from "express";
import { PrismaClient } from "@prisma/client";
const app = express();
const prisma = new PrismaClient();
app.use(express.json());
app.get("/users", async (req, res) => {
const users = await prisma.user.findMany();
res.json(users);
});
app.listen(3000, () =>
console.log("REST API sunucusu hazır: http://localhost:3000")
);
This code defines a GET /users route.
Start it with the command:
npx ts-node src/index.ts
The server starts and responds to the /users request.
| HTTP Method | Route / Endpoint | Description |
|---|---|---|
| GET | /feed | Lists all published posts. |
| GET | /post/:id | Returns the post with the specified ID. |
| POST | /user | Creates a new user. |
| POST | /post | Creates a new post (draft). |
| PUT | /post/publish/:id | Publishes the post (published = true). |
| DELETE | /post/:id | Deletes the specified post. |
Code example (part of it):
// Yeni rota örneği
app.put("/post/publish/:id", async (req, res) => {
const { id } = req.params;
const post = await prisma.post.update({
where: { id: Number(id) },
data: { published: true }
});
res.json(post);
});
This code makes a post "published=true".
â“ Frequently Asked Questions (FAQ)
- Why use Prisma?
Prisma provides type safety and allows you to work with modeling rather than writing SQL directly.
- Why was Docker recommended?
Ideal for quickly starting and cleanly uninstalling PostgreSQL in a development environment.
- Is TypeScript mandatory?
No, it is not mandatory, but it is very useful for type safety and code readability in REST APIs.
- Are the routes ready or should they be expanded?
This guide provides basic CRUD routes. If you wish, advanced topics such as authentication (JWT) and error management can be added.
ðŸ Result
In this guide, you have seen step by step how to install a REST API with TypeScript + Prisma + PostgreSQL and how to use it. You can write your codes faster and manage database queries more securely. 🚀 You can immediately implement these techniques on your GenixNode platform!

