Quick start with database functionality
This quick start guide will introduce you to the key concepts and help you with your first steps in working with databases in PyCharm.
Before you start
Enable the Database Tools and SQL plugin
This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in PyCharm by default. If the relevant features are not available, make sure that you did not disable the plugin.
Press Ctrl+Alt+S to open settings and then select
.Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.
Step 1. Create a data source
Depending on a database vendor (MySQL, PostgreSQL, Oracle, and so on), you need to create a corresponding data source to use it to connect to the database.
- Data source
Data source is a connection configuration. It stores a list of connection details that are used to establish connection to a database. For example, host, port, database name, driver, SSH and SSL configuration settings, and so on. In data source settings, you can also select databases and schemas for introspection and display in Database tool window, and change the driver for your connection.
Find more information about data sources in Data sources.
In this tutorial, you will set up a demo data source and connect to a demo database. To do this, you can either use the PostgreSQL or MySQL sample databases deployed remotely or download and use the SQLite sample database file.
You can also set up this demo data source using the connection credentials of a database you have setup and running somewhere else. To do this and connect to your database, enter the credentials or its JDBC URL in the corresponding fields of Data Source and Drivers dialog that is described in this tutorial.
In PyCharm, go to Database tool window ( or ⌘ 1) .
Click the Create data source link.
Navigate to Data Source and select PostgreSQL.
PyCharm will open a Data Source and Drivers dialog with the settings of your new data source.

In PyCharm, go to Database tool window ( or ⌘ 1) .
Click the Create data source link.
Navigate to Data Source and select MySQL.
PyCharm will open a Data Source and Drivers dialog with the settings of your new data source.

Prepare the SQLite sample database file by downloading from a GitHub repository, then copy the downloaded file to clipboard.
In PyCharm, paste the SQLite database file to the IDE project root directory.
To do that, in the Project tool window ( or Alt+1) , click any area and press Ctrl+V. In the Copy dialog that appears, click OK.
Go to Database tool window ( or ⌘ 1) .
Click the Create data source link.
Navigate to Data Source and select SQLite.
PyCharm will open a Data Source and Drivers dialog with the settings of your new data source.

Then, set up your new data source with the connection settings of your database.
In the Name field of the dialog, enter your new data source's name. For example,
Demo DB
.If there is a
Download missing driver files link at the bottom of the connection settings area, click it to download the JDBC driver.
In the Host field, enter the following server address:
postgresql.datagrip-dbs.intellij.netMake sure that the Port field contains the PostgreSQL default value:
5432
.In the User field, enter the demo user name:
datagrip
.In the Password field, enter the password:
datagrip
.In the Database field, enter the database name:
datagrip
.To make sure that the connection can be successfully established, click the Test Connection link at the bottom of the connection details area.
In the Schemas tab, make sure that the
datagrip
database that you will work with is selected.Click OK to create the data source.
Once the data source is created, it appears in the Database tool window.

In the Name field of the dialog, enter your new data source's name. For example,
Demo DB
.If there is a
Download missing driver files link at the bottom of the connection settings area, click it to download the JDBC driver.
In the Host field, enter the following server address:
mysql.datagrip-dbs.intellij.netMake sure that the Port field contains the MySQL default value:
3306
.In the User field, enter the demo user name:
datagrip
.In the Password field, enter the password:
datagrip
.In the Database field, enter the database name:
datagrip
.To make sure that the connection can be successfully established, click the Test Connection link at the bottom of the connection details area.
In the Schemas tab, make sure that the
datagrip
database that you will work with is selected.Click OK to create the data source.
Once the data source is created, it appears in the Database tool window.

In the Name field of the dialog, enter your new data source's name. For example,
Demo DB
.If there is a
Download missing driver files link at the bottom of the connection settings area, click it to download the JDBC driver.
In the File field, enter the path to your database file in the project root:
demo_db.sqlite
.ALternatively, click
Open and navigate to the file.
To make sure that the connection can be successfully established, click the Test Connection link at the bottom of the connection details area.
In the Schemas tab, make sure that the
main
database that you will work with is selected.Click OK to create the data source.
Once the data source is created, it appears in the Database tool window.

Step 2. Write and run SQL queries
With the Database Tools and SQL plugin, you can write SQL and NoSQL code in query consoles that are attached to data sources. When you create a new data source, the IDE creates a default query console for that data source.
- Query console
Query consoles are SQL files in which you can compose and execute SQL statements. Unlike the usual SQL files, they are attached to your data source by default.
For more information about query consoles, refer to the Query consoles topic.
In this tutorial, we will run simple queries in the default query console that PyCharm creates and opens upon the new data source creation.
In the default query console, enter the following SQL query:
SELECT * FROM actor;To run the query, click
Execute on the editor toolbar.
PyCharm will run the query and display query result set in the data editor tab of Services tool window ( or Alt+8) for you to analyze.
At this step, no schema is selected as the current one in the <schema> dropdown on the console toolbar. However, the query can still be executed because PyCharm will use the default schema of the data source.
For more information about how to set a default schema, refer to the Schemas topic.
In the default query console, enter the following SQL query:
SELECT * FROM actor;To run the query, click
Execute on the editor toolbar.
PyCharm will run the query and display query result set in the data editor tab of Services tool window ( or Alt+8) for you to analyze.
At this step, no schema is selected as the current one in the <schema> dropdown on the console toolbar. However, the query can still be executed because PyCharm will use the default schema of the data source.
For more information about how to set a default schema, refer to the Schemas topic.
In the default query console, enter the following SQL query:
SELECT * FROM actor;To run the query, click
Execute on the editor toolbar.
PyCharm will run the query and display query result set in the data editor tab of Services tool window ( or Alt+8) for you to analyze.
At this step, no schema is selected as the current one in the <schema> dropdown on the console toolbar. However, the query can still be executed because PyCharm will use the default schema of the data source.
For more information about how to set a default schema, refer to the Schemas topic.
Step 3. Explore the database
Now you can explore your database and database objects in a tree view and also open the objects to view their data. With the Database Tools and SQL plugin, you can do this in Database tool window.
- Database Explorer
Database tool window is a tool window that displays the database structure as a tree with nodes. You can create folders and move data sources to them, assign colors to data sources, filter the database objects, and so on.
For more information about the tool window, refer to the Database tool window topic. For more information about the tree icons, refer to the Data sources and their elements chapter.
For example, let us navigate to the actor table and its columns.
In the Database tool window, double-click the Demo_DB data source name to expand the list of introspected databases.
Then, double-click the nodes or click the
arrow icon near them to expand the list of items: .
To view the actor table data, double-click its name.
PyCharm will open the actor table in a data editor tab.
In the Database tool window, double-click the Demo_DB data source name to expand the list of introspected schemas.
Then, double-click the nodes or click the
arrow icon near them to expand the list of items: .
To view the actor table data, double-click its name.
PyCharm will open the actor table in a data editor tab.
In the Database tool window, double-click the Demo_DB data source name to expand the list of introspected . schemas
Then, double-click the nodes or click the
arrow icon near them to expand the list of items: .
To view the actor table data, double-click its name.
PyCharm will open the actor table in a data editor tab.
Step 4. View, sort, and filter the data
With the Database Tools and SQL plugin, you can view and edit the data of database tables and views in the data editor and viewer.
- Data editor and viewer
The data editor and viewer, or data editor, provides a user interface for working with data. In the data editor, you can sort, filter, add, edit, and remove the data as well as perform other associated tasks.
For more information about data editor, refer to the Data editor and viewer topic.
In this tutorial, we will sort the first_name column of actor table alphabetically and will also filter the last_name column to see the last names that start with JO
.
To sort the data, in the data editor tab, click the arrows icon in the first_name column header. PyCharm will send the
ORDER BY
query to the database and display its result in the editor.For more information about sorting data on the database side and on the client side, refer to the Sort data topic.
To filter the data, in the data editor tab, click
Local Filter in the last_name column header. Enter
JO
in the search field and select the Value checkbox, then click any area in the data editor.PyCharm will filter the data and display the result.
For more information about filtering data in the IDE, refer to the Filter data topic.
To sort the data, in the data editor tab, click the arrows icon in the first_name column header. PyCharm will send the
ORDER BY
query to the database and display its result in the editor.For more information about sorting data on the database side and on the client side, refer to the Sort data topic.
To filter the data, in the data editor tab, click
Local Filter in the last_name column header. Enter
JO
in the search field and select the Value checkbox, then click any area in the data editor.PyCharm will filter the data and display the result.
For more information about filtering data in the IDE, refer to the Filter data topic.
To sort the data, in the data editor tab, click the arrows icon in the first_name column header. PyCharm will send the
ORDER BY
query to the database and display its result in the editor.For more information about sorting data on the database side and on the client side, refer to the Sort data topic.
To filter the data, in the data editor tab, click
Local Filter in the last_name column header. Enter
JO
in the search field and select the Value checkbox, then click any area in the data editor.PyCharm will filter the data and display the result.
For more information about filtering data in the IDE, refer to the Filter data topic.
Next steps
After going through this tutorial, you can create a connection configuration and connect to a database using its JDBC URL, explore the connected database, run SQL queries in query consoles, and view the query results. Also, you can view, sort, and filter database object data.
To learn more about the IDE, consider the following topics and sections:
Use the instructions for various database vendors to create data sources for your databases. | |
Learn about the features you can use for viewing and managing data of database objects, query result sets, and DSV files. | |
This topic gives an idea of what the query console is and how to work with it in the IDE. | |
This topic describes the ways of viewing query results. | |
Find descriptions of the code editor features that you can use in query consoles, SQL files, and scratch files. | |
Learn how to export data and database objects. | |
Learn how to view diagrams of your databases and schemas. |