Main Content

Choose How to Store Credentials for Database Connections

Database Toolbox provides options for you to store and then access your username and password when making database connections. These options offer secure methods for managing credentials depending on your work environment. You can:

  • Store your credentials with your data source when using the Database Explorer app.

  • Store your credentials in your MATLAB® vault using setSecret.

Store Credentials with Database Explorer

If you prefer to have MATLAB® manage your user credentials and you are not writing deployable code in a desktop environment, then you can store your credentials with your data source by using Database Explorer to connect to a database. Once you store them, you do not have to provide them each time you connect. For example, to store your credentials with a PostgreSQL® data source, follow these steps:

  1. Open the Database Explorer app by clicking the Apps tab on the MATLAB Toolstrip. Then, on the right of the Apps section, click the Show more arrow to open the apps gallery. Under Database Connectivity and Reporting, click Database Explorer. Alternatively, enter databaseExplorer at the command line.

  2. In the Data Source section, select Configure Data Source > PostgreSQL.

    Configure PostgreSQL native interface data source

    The PostgreSQL Data Source Configuration dialog box opens.

    PostgreSQL Data Source Configuration dialog box

  3. Enter the name of your data source in the Name box.

  4. Store your user credentials by clicking the Set Credentials button.

Store Credentials in Your MATLAB Vault

If you prefer to manage your user credentials or you are writing deployable code in a non-desktop environment, then you can store your credentials in your MATLAB vault. Use the setSecret function to store them and use the getSecret function to retrieve them. The following example shows how to connect to a PostgreSQL database by specifying the data source, username, and password.

datasource = "PostgreSQLDataSource";
setSecret("usernamepostgresql");
setSecret("passwordpostgresql");
conn = postgresql(datasource,getSecret("usernamepostgresql"),getSecret("passwordpostgresql"));

See Also

Functions

Apps

Topics