Accessing databases in Cursor AI software is not a built-in direct feature, because Cursor is mainly used as an AI-driven code editor, focusing on code generation, completion and editing, rather than traditional database management tools such as DBeaver or pgAdmin) provides a graphical interface to access the database. However, you can implement database connections and operations by writing code and combining Cursor's AI capabilities. The following are the specific steps and methods.
1. Determine your development environment and database type
First, clarify the type of database you want to access (such as MySQL, PostgreSQL, SQLite, Snowflake, etc.) and the corresponding programming language (such as Python, JavaScript, Java, etc.).
Make sure that the necessary database drivers or dependency libraries are installed on your local or remote environment.
2. Install the necessary dependencies
Add the required libraries for database connection to your project. For example:
Python: pip install psycopg2
(PostgreSQL), pip install mysql-connector-python
(MySQL).
Node.js: npm install pg
(PostgreSQL), npm install mysql
(MySQL).
Open the terminal (Ctrl+J or Cmd+J) in Cursor and run the above command to install the dependencies.
3. Use Cursor's AI function to generate database connection code
step:
Create a new file in Cursor, such as db_connect.py
(Python) or db_connect.js
(Node.js).
Open the AI dialogue window (shortcut keys Ctrl+L or Cmd+L).
Enter a natural language prompt, for example:
"Help me connect to the PostgreSQL database using Python. The host is localhost, port 5432, the database name is mydb, the user is admin, and the password is 123456."
"Use Node.js to connect to the MySQL database, the host is 127.0.0.1, the database name is testdb, the user is root, and the password is password."
Cursor will generate the corresponding code, for example:
Python example (PostgreSQL) :
import psycopg2 # Connect to database conn = psycopg2.connect( host="localhost", port="5432", database="mydb", user="admin", password="123456" ) # Create cursor cursor = conn.cursor() # Execute the query cursor.execute("SELECT * FROM my_table") rows = cursor.fetchall() for row in rows: print(row) # Close the connection cursor.close() conn.close()
Node.js example (MySQL) :
const mysql = require('mysql'); // Create a connection const connection = mysql.createConnection({ host: '127.0.0.1', user: 'root', password: 'password', database: 'testdb' }); // Connect to the database connection.connect(err => { if (err) throw err; console.log('Connected to MySQL database!'); // Execute query connection.query('SELECT * FROM my_table', (err, results) => { if (err) throw err; console.log(results); connection.end(); }); });
Click the "Apply" button to apply the code to the file.
4. Run the code to access the database
After saving the file, run the code in Cursor's terminal:
Python: python db_connect.py
Node.js: node db_connect.js
If the configuration is correct, you will see the results of the database query output to the terminal.
5. Enhanced database operations (optional)
Provide the context : Save your database schema (schema) as a .md
file (such as schema.md
), and the content can be a DDL of the table (such as a CREATE TABLE
statement). Reference it with @schema.md
in an AI conversation to let Cursor understand your database structure.
Generate complex queries : Enter a query like "According to schema.md, help me write a query to count the number of each category in my_table", and Cursor will generate the corresponding SQL code.
Debugging and Optimization : If there is any problem with the code, you can highlight the code section and press Ctrl+K (or Cmd+K) to request AI repair or optimization.
There is no built-in database GUI : Cursor currently does not support graphical interfaces that directly view database structures or execute queries. If you need this kind of functionality, you can use external tools such as DBeaver or SQL extensions for VS Code and then write and debug code in Cursor.
Dependence code capability : Accessing a database requires you to have basic programming knowledge, or rely on Cursor's AI to generate the correct code.
Community advice : Some users in the Cursor forum have proposed to add built-in SQL/NoSQL database connection functionality (such as forum.cursor.com posts), but as of now (my knowledge is constantly updated), this has not been implemented.
Suppose you have a schema.md
file in your project:
# Database Schema ## Table: users - id: INTEGER PRIMARY KEY - name: VARCHAR(255) - email: VARCHAR(255)
In the AI conversation, enter: "Query all names and emails in the users table using Python according to @schema.md."
Cursor may generate:
import psycopg2 conn = psycopg2.connect("dbname=mydb user=admin password=123456 host=localhost port=5432") cursor = conn.cursor() cursor.execute("SELECT name, email FROM users") for row in cursor.fetchall(): print(row) conn.close()