How to create a Postgres database for a Claude Code or Cursor project
Updated
Create a Kaiva Cloud database and put its connection string in your project's .env as DATABASE_URL. Your ORM's migrations create the tables, and Claude Code or Cursor reads them through the database's managed MCP server, with nothing to run on your machine.
With Kaiva Cloud
Create a database in the Kaiva Cloud console and copy its connection string into .env as DATABASE_URL. Its tables become tools for Claude Code, Cursor and any other AI client through a managed MCP server, so there is no database or MCP server to run yourself. The database and its files are stored in the EU.
The steps below set up the same thing on your own machine.
Step by step
- Create the databaseOn your machine, run Postgres in Docker with the command below, or use Postgres.app on a Mac. Or create one at any hosted provider. The agent can run these commands for you in its terminal.
- Put the URL in .envAdd DATABASE_URL=postgresql://... to .env, and .env to .gitignore. Tell the agent the variable's name, not its value.
- Create the tables with migrationsAsk the agent to write the schema with your ORM, such as Prisma, Drizzle or SQLAlchemy, and to run its migration command. Every change is then a file in your repository you can review.
- Let the agent read the databaseAdd Kaiva Postgres MCP to Claude Code or Cursor with the database's URL. The agent can then list the tables, read a table's columns, keys and indexes, and run read-only queries.
- Check itAsk the agent which tables exist and how many rows each has. It answers from the database, not from your code.
Postgres in Docker, then the MCP server in Claude Code:
docker run -d --name shop-db -e POSTGRES_PASSWORD=dev -p 5432:5432 postgres:17
claude mcp add postgres -- npx -y @kaiva/postgres-mcp postgresql://postgres:dev@localhost:5432/postgresIn Cursor
Add the server to .cursor/mcp.json in your project. Cursor starts it when you open the project.
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@kaiva/postgres-mcp", "postgresql://postgres:dev@localhost:5432/postgres"]
}
}
}Keep writes in migrations
The agent changes the schema through migration files you can read and commit, and your app changes data through its code. The MCP server only reads: one statement per call, in a read-only transaction that is always rolled back.
On your own machine the postgres login is fine, and Kaiva Postgres MCP warns that it is a superuser. For a database anywhere else, give it a login that can only read.
When others use it
Use a hosted database for anything other people use, with one login for your app and a read-only one for AI. To use the same tables from Claude.ai or ChatGPT, or with your team, Kaiva Bridge serves them as a managed MCP server. In the project folder, run npx @kaiva/cli bridge push mydb --db --db-env AI_DATABASE_URL, where AI_DATABASE_URL holds the read-only login's connection string.
Kaiva Postgres MCP is free and open source. Kaiva Bridge's Free plan includes 10 MCP servers and 200,000 tool calls a month, with no card.
Other ways to do it
- A database the agent creates with no accountPrisma Postgres (npx create-db) and Neon can create a temporary Postgres database with one command and no sign-up, and you claim it to keep it.
- Your host's own MCP serverSupabase and Neon publish MCP servers that let the agent create tables and run SQL itself. Useful early on; review what it changes.
- SQLiteFor a small app with one user, a SQLite file needs no server at all. Move to Postgres when several people or services share the data.
Questions
- Should the agent create tables itself?
- Let it write migration files and run them, so each change is in your repository. A read-only MCP server keeps one-off changes out of the chat.
- Can Claude Code and Cursor use the same database?
- Yes. Each starts its own copy of Kaiva Postgres MCP with the same URL.
- Does the database URL reach the AI model?
- No. It stays in your MCP configuration on your machine. The model sees the tools and what they return.
- Can I use a hosted database?
- Yes. Put sslmode=require in the URL. If the connection fails on the certificate, add sslrootcert= and the path to your provider's CA file.
- Is it free?
- Kaiva Postgres MCP is free and open source, under the MIT licence.