PyCharm 2023.3 Help

MongoDB

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 aren't available, make sure that you didn't disable the plugin.

  1. Press Control+Alt+S to open the IDE settings and then select Plugins.

  2. Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.

Official documentation and software

This topic presents a general procedure on how you can create a data source for the connection to your MongoDB database in PyCharm, and run a test connection. It is assumed that you already have the necessary prerequisites and the database is up and running. For example, if you want to run a database on your machine and connect to that database, the corresponding DBMS software must be installed on the machine first.

Connect to a MongoDB database

  1. To connect to the database, create a data source that will store your connection details. You can do this using one of the following ways:

    • In the main menu, go to File | New | Data Source and select MongoDB.

    • In the Database tool window ( View | Tool Windows | Database) , click the New icon (the New icon) in the toolbar. Navigate to Data Source and select MongoDB.

    Create a new data source
  2. In the General tab of Data Sources and Drivers dialog right pane, specify the driver and connection type.

    1. In the Driver list, leave the default driver option, unless another driver is required for your connection.

    2. From the Connection type list, select the connection type depending on the connection details that you have:

      For the URL only connection type, the JDBC URL that you enter is used as is.

      For the other connection types, the JDBC URL is broken down into connection details. You can either specify them separately and use the automatically generated URL, or you can enter the URL directly in the corresponding field.

    General tab of the Data Sources and Drivers dialog
  3. Check if there is a Download missing driver files link at the bottom of the connection settings area. Click this link to download drivers that are required to interact with a database. For a direct download link, refer to the JetBrains JDBC drivers page.

    Location for the downloaded JDBC drivers is the PyCharm configuration directory.

    The Download missing driver files link

    You can also use your drivers for the database instead of the provided ones. For more information about connecting to a database with your driver, refer to Add a user driver to an existing connection.

    If there is no Download missing driver files link, then you already have the required drivers.

  4. Specify the database connection details. Alternatively, paste the JDBC URL in the URL field.

    1. In the Host field, type your server address.

    2. In the Port field, type the port of MongoDB. The default port is 27017.

    3. From the Authentication list, select the authentication method that you want to use to authenticate the connection. The following options are available:

      • User & Password: by using your login and password.

      • MongoDB Authentication options:

        • SCRAM-SHA-1: authenticate using User, Password, Authentication database and the SHA-256 hashing function.

        • SCRAM-SHA-256: authenticate using User, Password, Authentication database and the SHA-1 hashing function.

        • AWS IAM: authenticate using AWS access key id, AWS secret access key, and AWS session token.

        • x.509: use x.509 certificate for authentication.

        • GSSAPI (Kerberos): use Kerberos for authentication.

        • Plain (LDAP): authenticate by proxying the authentication request to a Lightweight Directory Access Protocol (LDAP) service.

      • No auth: authentication is not required.

    4. In the User and Password fields, type your user credentials.

      To use no password, leave the Password field empty.

      To delete a once entered password, right-click the Password field and select Set Empty.

      the Set Empty context menu action
    5. In the Database field, type the database name to which you want to connect.

    6. In the URL field, PyCharm generates the JDBC URL automatically using the values of other connection settings.

      If you need to use a JDBC URL with certain additional settings, paste it in the URL field. The general URL to use is as follows:

      • Format: mongodb://<host_address>:<port_number>/<database_name>

      • Example: mongodb://127.0.0.1:27017/myDatabase

      For more information about the URL format, refer to the MongoDB official documentation.

    Database connection details
    1. In the Host field, type your server address.

    2. In the Port field, type the port of MongoDB. The default port is 27017.

    3. From the Authentication list, select the authentication method that you want to use to authenticate the connection. The following options are available:

      • User & Password: by using your login and password.

      • MongoDB Authentication options:

        • SCRAM-SHA-1: authenticate using User, Password, Authentication database and the SHA-256 hashing function.

        • SCRAM-SHA-256: authenticate using User, Password, Authentication database and the SHA-1 hashing function.

        • AWS IAM: authenticate using AWS access key id, AWS secret access key, and AWS session token.

        • x.509: use x.509 certificate for authentication.

        • GSSAPI (Kerberos): use Kerberos for authentication.

        • Plain (LDAP): authenticate by proxying the authentication request to a Lightweight Directory Access Protocol (LDAP) service.

      • No auth: authentication is not required.

    4. In the User and Password fields, type your user credentials.

      To use no password, leave the Password field empty.

      To delete a once entered password, right-click the Password field and select Set Empty.

      the Set Empty context menu action
    5. In the Database field, type the database name to which you want to connect.

    6. In the URL field, PyCharm generates the JDBC URL automatically using the values of other connection settings.

      If you need to use a JDBC URL with certain additional settings, paste it in the URL field. The general URL to use is as follows:

      • Format: mongodb+srv://<host_address>/<database_name>

      • Example: mongodb+srv://127.0.0.1/myDatabase

      For more information about the URL format, refer to the MongoDB official documentation.

    Database connection details
    1. From the Authentication list, select the authentication method that you want to use to authenticate the connection. The following options are available:

      • User & Password: by using your login and password.

      • MongoDB Authentication options:

        • SCRAM-SHA-1: authenticate using User, Password, Authentication database and the SHA-256 hashing function.

        • SCRAM-SHA-256: authenticate using User, Password, Authentication database and the SHA-1 hashing function.

        • AWS IAM: authenticate using AWS access key id, AWS secret access key, and AWS session token.

        • x.509: use x.509 certificate for authentication.

        • GSSAPI (Kerberos): use Kerberos for authentication.

        • Plain (LDAP): authenticate by proxying the authentication request to a Lightweight Directory Access Protocol (LDAP) service.

      • No auth: authentication is not required.

    2. In the User and Password fields, type your user credentials.

      To use no password, leave the Password field empty.

      To delete a once entered password, right-click the Password field and select Set Empty.

      the Set Empty context menu action
    3. In the URL field, PyCharm generates the JDBC URL automatically using the values of other connection settings.

      If you need to use a JDBC URL with certain additional settings, paste it in the URL field.

      For more information about the URL format, refer to the MongoDB official documentation.

    Database connection details

    For the reference information about connection settings and properties on the General and other tabs of Data Sources and Drivers dialog (Shift+Enter), see Connection settings and DBMS-specific properties.

  5. Ensure that the connection to the database can be established using the provided details. To do that, click the Test Connection link at the bottom of the connection details area.

    Test Connection link

    In case of any connection issues, refer to the Cannot connect to a database page.

  6. (Optional) By default, only the default database is introspected and available to work with. If you also want to work with other databases, in the Schemas tab, select them for the introspection.

    Schemas tab of the Data Sources and Drivers dialog
  7. Click OK to create the data source.

  8. Find your new data source in the Database tool window (Shortcut is missing) .

    • For more information about the Database tool window, see the corresponding reference topic.

    • For more information about working with database objects in PyCharm, refer to Database objects.

    • To write and run queries, open the default query console by clicking the data source and pressing F4.

    • To view and edit data of a database object, open Data editor and viewer by double-clicking the object.

Connection settings and DBMS-specific properties

Connection settings

For the reference information about connection settings (for example, Host, Port, and so on) on the General and other tabs of Data Sources and Drivers dialog (Shift+Enter), see Data source settings.

DBMS-specific properties

General tab

Item

Description

Authentication database

Name of the authentication database that is associated with your username. If the authentication database differs from the database to which you want to connect, specify the authentication database with the authSource parameter in the URL. If no authentication database is specified, MongoDB uses the admin database by default.

For example, you want to connect to the local database but your authentication database is admin. In this case, the connection URL will look like: mongodb://localhost:27017/local?authSource=admin.

The property is used for the SCRAM-SHA-1 and SCRAM-SHA-256 authentication method.

AWS access key id

The connection properties used for the MongoDB AWS IAM authentication method.

AWS session token

AWS secret access key

Principal

The connection properties used for the MongoDB GSSAPI (Kerberos) authentication method.

For more information, refer to MongoDB official documentation on this method.

Service name

Service realm

Canonicalize host name

Last modified: 20 September 2023