Main Content

Connect to Database

To connect to a database from MATLAB®, install an ODBC or JDBC driver and create a data source. Or, you can connect to MySQL® or PostgreSQL databases using the native interfaces. For details about driver installation and data source setup, see Configure Driver and Data Source. If you do not have an installed database and want to store relational data quickly, you can use the MATLAB interface to SQLite. For details, see Interact with Data in SQLite Database Using MATLAB Interface to SQLite.

You can connect to a database using the Database Explorer app or the command line. These two options enable you to perform different actions. For details about deciding which option to use, see Connection Options.

Database Explorer App Connection Workflow

Use these steps as a general workflow for creating a database connection using the app.

  • In the Data Source section of the Database Explorer tab, click Configure Data Source and select the appropriate option for configuring an ODBC, JDBC, or native data source.

  • In the Connections section, click Connect and select the configured data source to create a database connection.

  • To close the database connection, in the Connections section, click Close Connection. If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from the Close Connection list.

Command Line Connection Workflow

Use these steps as a general workflow for creating a database connection at the command line.

  • Configure an ODBC data source using the configureODBCDataSource function, or configure a JDBC data source using the databaseConnectionOptions function.

  • Create a database connection by completing one of these steps:

    • Create an ODBC database connection using the odbc function.

    • Create an ODBC or JDBC database connection using the database function.

    • Create a MySQL database connection using the MySQL native interface with the mysql function.

    • Create a PostgreSQL database connection using the PostgreSQL native interface with the postgresql function.

  • If you do not configure a data source using a data source name (DSN), you can create a DSN-less connection using a connection string with the odbc function.

  • Close the database connection using the close function.

Database List

After choosing to use the Database Explorer app or the command line, connect to your database by following the steps for the corresponding database and driver type, as given in this table.

DatabaseDriver or InterfaceConnection Topics

Microsoft Access®

ODBC

Connect to Access Using Database Explorer App

Connect to Access Using ODBC Driver and Command Line

Microsoft® SQL Server®

ODBC

Connect to SQL Server Using Database Explorer App

Connect to SQL Server Using ODBC Driver and Command Line

Connect to SQL Server Using DSN-Less Connection String and Command Line

JDBC

Connect to SQL Server Using Database Explorer App

Connect to SQL Server Using JDBC Driver and Command Line

Oracle®

ODBC

Connect to Oracle Using Database Explorer App

Connect to Oracle Using ODBC Driver and Command Line

JDBC

Connect to Oracle Using Database Explorer App

Connect to Oracle Using JDBC Driver and Command Line

MySQL

ODBC

Connect to MySQL Using Database Explorer App

Connect to MySQL Using ODBC Driver and Command Line

Connect to MySQL Using DSN-Less Connection String and Command Line

MySQL native interface

Connect to MySQL Using Database Explorer App

Connect to MySQL Using Command Line

JDBC

Connect to MySQL Using Database Explorer App

Connect to MySQL Using JDBC Driver and Command Line

PostgreSQL

ODBC

Connect to PostgreSQL Using Database Explorer App

Connect to PostgreSQL Using ODBC Driver and Command Line

Connect to PostgreSQL Using DSN-Less Connection String and Command Line

PostgreSQL native interface

Connect to PostgreSQL Using Database Explorer App

Connect to PostgreSQL Using Command Line

JDBC

Connect to PostgreSQL Using Database Explorer App

Connect to PostgreSQL Using JDBC Driver and Command Line

SQLite

JDBC

Connect to SQLite Using Database Explorer App

Connect to SQLite Using JDBC Driver and Command Line

None

To create an SQLite connection when no driver or database installation is required, use the sqlite function to create a new SQLite database file or connect to an existing SQLite database file.

All other ODBC-compliant or JDBC-compliant databases

ODBC or JDBC

Other ODBC-Compliant or JDBC-Compliant Databases

See Also

| | | |

Related Topics