This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

PostgreSQL ODBC for Windows

This tutorial shows how to set up a data source and connect to a PostgreSQL database using the Database Explorer app or the command line. This tutorial uses the PostgreSQL ANSI(x64) driver to connect to a PostgreSQL 9.2 database.

Step 1. Verify the driver installation.

The ODBC driver is typically preinstalled on your computer. For details about the driver installation or troubleshooting the installation, contact your database administrator or refer to your database documentation on ODBC drivers. For information about the Microsoft® ODBC Data Source Administrator, see Driver Installation.

Note:

Database Toolbox™ no longer supports connecting to a database using a 32-bit driver. Use the 64-bit version of PostgreSQL. If you have issues working with the ODBC driver, use the JDBC driver instead. For details, see PostgreSQL JDBC for Windows. For details about working with the 64-bit version of Windows®, see https://www.mathworks.com/products/matlab/preparing-for-64-bit-windows.html.

Step 2. Set up the data source using the Database Explorer app.

The Database Explorer app accesses the Microsoft ODBC Data Source Administrator automatically when you configure an ODBC data source. Alternatively, you can access the Microsoft ODBC Data Source Administrator using the configureODBCDataSource function.

  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 > Configure ODBC data source.

    In the ODBC Data Source Administrator dialog box, you define the ODBC data source.

    Tip

    When setting up an ODBC data source, you can specify a user data source name (DSN) or a system DSN. A user DSN is specific to the person logged into a machine. Only this person sees the data sources that are defined on the user DSN tab. A system DSN is not specific to the person logged into a machine. Any person who logs into the machine can see the data sources that are defined on the system DSN tab. Your ability to set up a user DSN or system DSN depends on the database and ODBC driver you are using. For details, contact the database administrator or refer to the ODBC driver documentation.

  3. Click the System DSN tab, and then click Add.

    The Create New Data Source dialog box opens and displays a list of installed ODBC drivers.

  4. Select the ODBC driver PostgreSQL ANSI(x64).

    Note

    The name of the ODBC driver can vary.

    Click Finish.

  5. In the PostgreSQL ANSI ODBC Driver (psqlODBC) Setup dialog box, fill out the boxes.

    • In the Data Source box, enter an appropriate name for the data source, such as PostgreSQL. You use this name to establish a connection to your database.

    • In the Description box, enter a description for this data source, such as PostgreSQL database.

    • In the Database box, enter the name of your database.

    • In the Server box, enter the name of your database server. Consult your database administrator for the name of your database server.

    • In the Port box, enter the port number. The default port number is 5432.

    • In the User Name box, enter your user name.

    • In the Password box, enter your password.

  6. Click Test to test the connection to your database. If your computer successfully connects to the database, the Connection Test dialog box opens and displays a message indicating the connection is successful.

  7. Click Save in the PostgreSQL ANSI ODBC Driver (psqlODBC) Setup dialog box. The ODBC Data Source Administrator dialog box shows the ODBC data source PostgreSQL30.

    After you complete the data source setup, connect to the PostgreSQL database using the Database Explorer app or the command line with the native ODBC connection.

Step 3. Connect using the Database Explorer app or the command line.

Connect to PostgreSQL Using Database Explorer App

  1. On the Database Explorer tab, in the Data Source section, click New Query.

  2. In the Connect to a Data Source dialog box, select the data source you defined from the Data Source list. Enter a user name and password, or leave these boxes blank if your database does not require them. Click Connect.

    The Catalog and Schema dialog box opens.

  3. Select the catalog and schema from the Catalog and Schema lists. Click OK.

    The Database Explorer app connects to the database and displays database tables in the Data Browser pane. A data source tab appears to the right of the pane. The title of the data source tab is the data source name that you defined during the setup. The data source tab contains empty SQL Query and Data Preview panes.

  4. Select tables in the Data Browser pane to query the database.

  5. Close the data source tab to close the SQL query and the database connection.

    Tip

    To close the database connection, close all tabs that have titles beginning with the name of the corresponding data source. For example, if the data source name is Postgresql, and two tabs named Postgresql and Postgresql1 are open, then close both tabs.

    To close all database connections, close the Database Explorer app.

Connect to PostgreSQL Using ODBC Driver and Command Line

  1. Connect to the database with the ODBC data source name. For example, this code assumes that you are connecting to a data source named PostgreSQL with the user name username and the password pwd.

    conn = database('PostgreSQL','username','pwd');
  2. Close the database connection.

    close(conn)

See Also

Apps

Functions

Related Topics