PostgreSQL
STDIOMCP server that connects to PostgreSQL database for AI model interaction.
MCP server that connects to PostgreSQL database for AI model interaction.
This project implements a Model Context Protocol (MCP) server that connects to a PostgreSQL database. It allows AI models to interact with your database through a standardized protocol.
npm install
The server reads database credentials from a .env file in the project root directory. You need to add your database credentials as a JSON string in the DB_CREDENTIALS environment variable:
.env file in the project root:touch .env
export DB_CREDENTIALS='{"DB_USER":"your-username","DB_PASSWORD":"your-password","DB_HOST":"your-host","DB_PORT":"5433","DB_NAME":"your-database"}'
If the .env file is not present or the credentials variable is not found, the server will automatically look for the credentials in your shell configuration files in the following order:
~/.zshrc~/.bashrc~/.bash_profile~/.profileThis is especially useful in environments where shell config files are not automatically sourced, such as the Cursor MCP environment.
To set up credentials in any of your shell config files:
nano ~/.zshrc # or nano ~/.bashrc
export DB_CREDENTIALS='{"DB_USER":"your-username","DB_PASSWORD":"your-password","DB_HOST":"your-host","DB_PORT":"5433","DB_NAME":"your-database"}'
The server will automatically detect and use these credentials when the .env file is not available.
You can also use a custom environment variable name instead of DB_CREDENTIALS by using the --credentials-var flag when starting the server:
node server.js --credentials-var MY_CUSTOM_DB_CREDS
In this case, you would define MY_CUSTOM_DB_CREDS in your .env file instead.
You can combine different command-line options as needed:
# Use custom credentials and enable verbose mode node server.js --credentials-var MY_CUSTOM_DB_CREDS --verbose # Short form also works node server.js -c MY_CUSTOM_DB_CREDS -v
Start the MCP server:
# Directly with Node.js node server.js # Or with npm npm start
By default, the server runs in silent mode, displaying only error messages. If you want to see all log messages, you can use the verbose flag:
# With verbose logging node server.js --verbose # Or with npm npm start -- --verbose
You can also use the short flag -v:
node server.js -v
The server will:
This server supports the Model Context Protocol (MCP) and integrates with Cursor AI.
This project includes a pre-configured .cursor/mcp.json file for automatic setup within Cursor.
To manually add this server to Cursor:
node /full/path/to/server.jsFor more information on MCP integration with Cursor, see the official documentation.
The server provides the following tools to AI models:
query: Execute SQL queries with retry logicThe server exposes database tables as resources, allowing AI models to:
The server includes:
Database Connection Failed
pg_isready -h localhost -p 5433.env file are correctpsql to verify credentialsEnvironment Variable Problems
.env file is in the project root directoryDB_CREDENTIALS is validnode -e "console.log(JSON.parse(process.env.DB_CREDENTIALS))" < .envNode.js Version Issues
node -vnvm install 20 && nvm use 20Server Not Showing in Cursor
.cursor/mcp.json file exists and is properly formatted"Failed to create client" Error
node server.js -vNo Tools Available in Cursor
Permission Denied Errors
GRANT SELECT ON ALL TABLES IN SCHEMA public TO username;"Relation does not exist" Errors
\dt tablename in psqlPerformance Issues
For additional help, you can run the server with verbose logging (-v flag) to see detailed error messages and operation logs.
MIT