This project is a Node.js server that connects to a PostgreSQL database. It includes utilities for AI and database interactions in natural language.
- ✅ Converts English questions to SQL queries using Google Gemini API
- ✅ Executes queries with read-only PostgreSQL user
- ✅ Prevents INSERT, UPDATE, DELETE, DROP, ALTER
- ✅ Minimal data exposure
- Node.js (Express)
- PostgreSQL
- Google Gemini API
- Copy
.env.exampleto.envand configure your environment variables. - Run
npm installto install dependencies. - Start the server using:
npm start
Ensure the following variables are set in your .env file:
GEMINI_API_KEY: Your Google Gemini API key.PG_HOST: Hostname or service name of your PostgreSQL instance.PG_USER: PostgreSQL username.PG_PASSWORD: PostgreSQL password.PG_DATABASE: Name of the PostgreSQL database.PG_PORT: Port number for PostgreSQL (default: 5432).PORT: Port number for the server (default: 3000).
Send a POST request to the /query endpoint with the following JSON payload:
{
"question": "Your English question here"
}The server will respond with the generated SQL query and the query results.
Example:
curl -X POST http://localhost:3000/query \
-H "Content-Type: application/json" \
-d '{"question": "List all records created in the last 30 days"}'Response:
{
"query": "SELECT * FROM records WHERE created_at >= NOW() - INTERVAL '30 days';",
"data": [ ... ]
}-
Build and start the Docker containers:
docker-compose up --build -d -
Access the server at
http://localhost:3000. -
Monitor logs using:
docker logs -f mcp_postgres_server -
To stop the containers:
docker-compose down