主要内容

PostgreSQL JDBCLinux

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. The tutorial uses the JDBC4 PostgreSQL Driver, Version 8.4 to connect to a PostgreSQL 9.2 database.

Step 1. Verify the driver installation.

If the JDBC driver for PostgreSQL is not installed on your computer, find the link on theDriver Installationpage to install the driver. Follow the instructions to download and install this driver on your computer.

Step 2. Set up the data source.

您使用数据库Explorer应用程序或命令行设置数据源。

Set Up Data Source Using Database Explorer App

  1. 通过单击该数据库Explorer应用程序AppsMATLAB上的标签®Toolstrip. Then, on the right of theAppssection, click theShow more画廊箭头打开应用程序。Under数据库连接和报告, 点击Database Explorer。Alternatively, enter数据库Explorerat the command line.

  2. 在里面数据源section, selectConfigure Data Source>Configure JDBC data source

    使用选定的配置JDBC数据源配置数据源选择

    The JDBC Data Source Configuration dialog box opens.

  3. 在里面Name框,输入数据源的名称。(此示例使用名为的数据源PostgreSQL。) You use this name to establish a connection to your database.

  4. From the小贩list, selectPostgreSQL

    JDBC数据源配置对话框,带有选定的PostgreSQL供应商

  5. 在里面驾驶员位置box, enter the full path to the JDBC driver file.

  6. 在里面Database框,输入数据库的名称。在里面服务器box, enter the name of your database server. Consult your database administrator for the name of your database server. In the端口号box, enter the port number.

  7. Under连接选项, 在里面Namecolumn, enter the name of an additional driver-specific option. Then, in the价值列,输入特定于驱动程序的选项的值。单击加号+指定其他特定于驱动程序的选项。

  8. ClickTest。The Test Connection dialog box opens. Enter the user name and password for your database, or leave these boxes blank if your database does not require them. ClickTest

    如果您的连接成功,则数据库资源管理器对话框显示一条消息,表明连接成功。否则,它将显示一个错误消息。

  9. Click节省。JDBC数据源配置对话框显示一条消息,指示数据源成功保存。关闭此对话框。

使用命令行设置数据源

  1. 为PostgreSQL数据库创建JDBC数据源。

    供应商=“ postgresql”;opts = databaseConnectionOptions(“ JDBC”,小贩);
  2. 设置JDBC连接选项。例如,此代码假设您正在连接到name namy naturePostgreSQL, full path of the JDBC driver file/home/user/DB_Drivers/postgresql-8.4-702.jdbc4.jar, 数据库名称toystore_doc, database serverdbtb00, and port number5432

    opts = setoptions(opts,。。。'DataSourCename',“ postgresql”,。。。'JDBCDRIVERLOCATION',"/home/user/DB_Drivers/postgresql-8.4-702.jdbc4.jar",。。。'数据库名称',"toystore_doc",'服务器',“ DBTB00”,。。。'PortNumber',5432);
  3. Test the database connection by specifying the user nameusername和passwordPWD,或者如果您的数据库不需要它们,则将这些参数留为空白。

    username ="username";密码=“ PWD”;status = testConnection(opts,username,password);
  4. 节省the JDBC data source.

    saveAsDataSource(opts)

After you complete the data source setup, connect to the PostgreSQL database using the Database Explorer app or the JDBC driver and command line.

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

使用数据库Explorer应用连接到PostgreSQL

  1. On theDatabase Explorer标签,在Connectionssection, clickConnect并选择连接的数据源。

  2. 在里面connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. ClickConnect

    目录和模式对话框打开。

  3. 从中选择目录和架构目录Schemalists. Click好的

    该应用连接到数据库,并在数据浏览器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 emptySQL查询Data Preview窗格。

  4. 数据浏览器pane to query the database.

  5. Close the data source tab to close the SQL query. In theConnectionssection, close the database connection by clickingClose Connection

    Note

    If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from theClose Connection列表。

Connect to PostgreSQL Using JDBC Driver and Command Line

  1. Connect to a PostgreSQL database using the configured JDBC data source, user nameusername, and passwordPWD

    datasource =“ postgresql”;username ="username";密码=“ PWD”;conn = database(datasource,username,password);
  2. Close the database connection.

    close(conn)

See Also

Apps

Functions

相关话题