Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyCharm Database Tools

PyCharm Database Tools

PyCharm provides powerful database management tools, supporting mainstream relational databases (MySQL, PostgreSQL, Oracle, etc.) as well as NoSQL (MongoDB).

We can connect, query, debug, and visualize data directly inside PyCharm.

Common shortcuts:

Operation Shortcut (Win/Linux) Shortcut (Mac)
New query console Alt+F4 ⌥F4
Execute SQL Ctrl+Enter ⌘Enter
Format SQL Ctrl+Alt+L ⌥⌘L
Jump to table definition Ctrl+B ⌘B
Data export Right click → Export Data Same as above

Why use PyCharm database tools?

Improve development efficiency

Powerful features

Seamless integration


How to configure a database connection?

1. Open the database tool window

In PyCharm, click the "Database" icon in the right sidebar (usually displayed as a small cylinder), or open the database tool window through the menu bar View → Tool Windows → Database.

Or through the menu bar View → Tool Windows → Database to open the database tool window.

2. Add a new data source

  1. Click the "+" button
  2. Select the database type you want to connect to (such as MySQL, PostgreSQL, etc.)
  3. Fill in the connection information:
  4. Host: database server address
  5. Port: database port
  6. User: username
  7. Password: password
  8. Database: the name of the database to connect to
  9. - Host: database server address - Port: database port - User: username - Password: password - Database: the name of the database to connect to -

    1. Click "Test Connection" to test the connection

    2. Click "Confirm" to save the configuration after the connection is successful

    3. Interface after successful connection

    After a successful connection, you can see in the database tool window:


    Detailed feature explanation

    1. Execute SQL query

    1. Right click the database connection
    2. Select "New" → "Query Console"
    3. Write SQL statements in the opened SQL console
    4. Click the execute button (green triangle) or use the shortcut (Ctrl+Enter) to execute the query

    2. View and edit data

    1. Expand the table list in the database tool window
    2. Double-click the table name to open the data view
    3. Can directly edit data in the table
    4. Click the submit button to save changes after modification

    3. Export and import data

    1. Right click the table or query result
    2. Select "Export to File" to export data as CSV, Excel, JSON, etc.
    3. Select "Import from File" to import data from a file

    4. Table structure management

    1. Right click the table
    2. Select "Modify Table" to modify the table structure
    3. Can add/delete columns, modify data types, set primary keys and foreign keys, etc.

    5. Database console

    PyCharm provides complete database console features, supporting:


    Advanced features

    1. Database diagram

    PyCharm can generate database relationship diagrams:

    1. Right click the database connection
    2. Select "Diagrams" → "Show Visualization"
    3. View the relationship diagram between tables

    2. Version control integration

    Database scripts can be version controlled together with the project:

    1. Create a database change script
    2. Commit to version control system
    3. Keep database schema in sync when collaborating as a team

    3. Integrate with Python code

    PyCharm lets you use database tools directly in Python code:

    1. Write SQL statements in Python files
    2. Use PyCharm's database support for syntax checking and autocompletion
    3. Execute SQL statements directly and view the results

    4. Database migration tool support

    PyCharm integrates popular database migration tools such as:


    Practical tips

    1. Keyboard shortcuts

    2. Code templates

    PyCharm provides various SQL code templates that quickly generate common SQL statement structures via abbreviations.

    3. Result set handling

    Query results can:

    4. Connection pool management

    PyCharm supports connection pool configuration, allowing you to set:


    Frequently Asked Questions

    1. What to do if the connection fails?

    2. How to view the execution plan?

    1. Write a query in the SQL console
    2. Right-click the query
    3. Select 'Explain Plan' to view the execution plan

    3. How to compare database schemas?

    1. Right click the database connection
    2. Select 'Compare With' -> select another database connection
    3. View the schema differences

    4. How to back up the database?

    1. Right click the database connection
    2. Select 'Dump with mysqldump' (or the export tool for the corresponding database)
    3. Select the export options and file location