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
通过单击该数据库Explorer应用程序AppsMATLAB上的标签®Toolstrip. Then, on the right of theAppssection, click theShow more画廊箭头打开应用程序。Under数据库连接和报告, 点击Database Explorer。Alternatively, enter
数据库Explorer
at the command line.在里面数据源section, selectConfigure Data Source>Configure JDBC data source。
The JDBC Data Source Configuration dialog box opens.
在里面Name框,输入数据源的名称。(此示例使用名为的数据源
PostgreSQL
。) You use this name to establish a connection to your database.From the小贩list, select
PostgreSQL
。在里面驾驶员位置box, enter the full path to the JDBC driver file.
在里面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.
Under连接选项, 在里面Namecolumn, enter the name of an additional driver-specific option. Then, in the价值列,输入特定于驱动程序的选项的值。单击加号+指定其他特定于驱动程序的选项。
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。
如果您的连接成功,则数据库资源管理器对话框显示一条消息,表明连接成功。否则,它将显示一个错误消息。
Click节省。JDBC数据源配置对话框显示一条消息,指示数据源成功保存。关闭此对话框。
使用命令行设置数据源
为PostgreSQL数据库创建JDBC数据源。
供应商=“ postgresql”;opts = databaseConnectionOptions(“ JDBC”,小贩);
设置JDBC连接选项。例如,此代码假设您正在连接到name namy nature
PostgreSQL
, 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);
Test the database connection by specifying the user name
username
和passwordPWD
,或者如果您的数据库不需要它们,则将这些参数留为空白。username ="username";密码=“ PWD”;status = testConnection(opts,username,password);
节省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
On theDatabase Explorer标签,在Connectionssection, clickConnect并选择连接的数据源。
在里面connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. ClickConnect。
目录和模式对话框打开。
从中选择目录和架构目录和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窗格。
在数据浏览器pane to query the database.
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
Connect to a PostgreSQL database using the configured JDBC data source, user name
username
, and passwordPWD
。datasource =“ postgresql”;username ="username";密码=“ PWD”;conn = database(datasource,username,password);
Close the database connection.
close(conn)