This repository demonstrates how to set up and use PostgREST to create a RESTful API from a PostgreSQL database.
- Automatic REST API generation from PostgreSQL database
- CRUD operations
- Secure access control
- PostgreSQL
- Docker (optional but recommended for easy setup)
-
Clone the repository:
git clone https://github.com/muthukumar89uk/go-restapi-postgrest.git
Click here to directly download it.
go mod tidy
-
Run the Server
go run .
-
The server will start on
http://localhost:8080
.
-
Download PostgREST:
Download the latest release of PostgREST from the official GitHub releases page.
-
Create a Configuration File:
Create a
postgrest.conf
file with the following content:db-uri = "postgres://postgres:mysecretpassword@localhost:5432/mydatabase" db-schema = "api" db-anon-role = "postgres"
-
Run PostgREST Using Docker Compose Yaml File
- Go to the docker-compose.yaml file directory then run the
docker-compose up
command. - This command will pull the necessary Docker images (if not already downloaded), create the containers, and start the services.
- Go to the docker-compose.yaml file directory then run the
-
Start PostgREST:
./postgrest postgrest.conf
-
Access the API:
The API will be available at
http://localhost:3000/<table-name>
. You can test the endpoints using tools like curl or Postman.Example Endpoints:
GET /tasks
- Retrieve all itemsGET /tasks/:id
- Retrieve an item by IDPOST /tasks
- Create a new itemPATCH /tasks/:id
- Update an existing itemDELETE /tasks/:id
- Delete an item
Example cURL requests:
-
Get all items:
GET http://localhost:3000/items
-
Create a new item:
POST http://localhost:3000/items
-
Update an existing item:
PUT http://localhost:3000/tasks/:id
-
Delete an item:
DELETE http://localhost:3000/tasks/:id