https://github.com/alamgir-ahosain/go-postgres-crud-rest-api
Build a CRUD REST API in Go (Golang) using PostgreSQL database and Gorilla Mux framework.
https://github.com/alamgir-ahosain/go-postgres-crud-rest-api
crud-api go golang gorilla-mux postgresql rest-api
Last synced: about 2 months ago
JSON representation
Build a CRUD REST API in Go (Golang) using PostgreSQL database and Gorilla Mux framework.
- Host: GitHub
- URL: https://github.com/alamgir-ahosain/go-postgres-crud-rest-api
- Owner: alamgir-ahosain
- Created: 2025-08-18T10:36:20.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-19T20:02:20.000Z (11 months ago)
- Last Synced: 2026-05-12T23:46:03.102Z (about 2 months ago)
- Topics: crud-api, go, golang, gorilla-mux, postgresql, rest-api
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## GO PostgreSQL CRUD REST API
Build a `CRUD` `REST API` in `Go (Golang)` using `PostgreSQL` database and `Gorilla/Mux` framework.
>For PostgreSQL
## Installation
1. **Clone the Repository**
```bash
https://github.com/alamgir-ahosain/go-postgres-crud-rest-api.git
```
2. **Install Dependencies**
```bash
go get github.com/lib/pq
go get -u github.com/gorilla/mux
go get github.com/joho/godotenv
```
## Database Setup
Login to PostgreSQL and create the database and table:
```sql
CREATE DATABASE goDB;
USE goDB;
CREATE TABLE users (
id SERIAL PRIMARY KEY,
sid VARCHAR(128) NOT NULL,
name VARCHAR(128) NOT NULL,
cgpa NUMERIC(5,2)
);
```
## Run the Application:
1. Start PostgreSQL server.
2. Update database connection in `db/postgreSql.go`
3. Configure `.env` in `config/.env`
4. Run the Project:
```go
go run main.go
```
## Project Structure
```plaintext
GO-POSTGRES-CRUD-REST-API
│── cmd/
│ │─── MyApp
│ │ ├── server.go # main serve file
│ │
│── internal/
│ ├── api/
│ │ ├── handlers/
│ │ │ ├── get_users/
│ │ │ │ ├── get_user_by_id.go # get user by ID function
│ │ │ │ ├── get_users.go # get all users function
│ │ │ │── create_user.go # create user function
│ │ │ │── delete_user.go # delete user function
│ │ │ │── update_user.go # update user function
│ │ ├── routes/
│ │ └── routes.go # all routes declared here
│ │
│ │── config/
│ │ │── .env # configuration environment
│ │
│ │── db/
│ │ │── postgreSql.go # postgreSQL database connection setup
│ │
│ │── models/
│ │ │── model.go # User model definition
│ │
│ │── services/
│ │ │── service.go # Helper function
│
│── main.go # Main entry file
│── go.mod # Go module definition
│── go.sum # Go dependencies checksums
│── README.md # Project documentation
```
---
## API endpoints table
| number | method | endpoint | description |
| ------ | ------ | -------------- | ------------------------------|
| 1 | GET | /users | get all users |
| 2 | GET | /users/{id} | get user by id |
| 3 | POST | /users | create new user |
| 4 | PUT | /users/{id} | full update user by id |
| 5 | DELETE | /users/{id} | delete user by id |
## Testing the CRUD API with Postman or Thunder Client( VS code extension)
#### 1. Get All Users
Method: GET
URL: http://localhost:8000/users
#### 2. Get a single User
Method: GET
URL: http://localhost:8000/users/1
#### 3. Create a new Users
Method: POST
URL: http://localhost:8000/users
#### 4. Update a User
Method: PUT
URL: http://localhost:8000/users/1
#### 5. Delete a User
Method: DELETE
URL: http://localhost:8000/users/1
---
# Thank You for Checking Out This Repository!
Your feedback is valuable! Please share your thoughts and suggestions for improvement via [GitHub Issues](https://github.com/alamgir-ahosain/go-postgres-crud-rest-api/issues).
# Contributing
Contributions are welcome! Feel free to fork the repo and create a pull request.