Assumptions
My primary focus is not on Matlab, but I used it for a while in the Uni. I have to support connecting Matlab installed on Windows machine with Postgres 9.2 database. I used the Matlab command window, so this have to be straightforward to do on you environment, assuming the database driver do not differ much in other environments.
Connection configuration
Connecting Matlab to Postgres requires to have JDBC driver, which you can download from here. Select current version or choose one appropriate for you OS from the Supported versions table. Put the jar file somewhere in your drive. I put mine at “C:\Program Files\MATLAB\R2011b\java\jar\postgresql-9.2-1002.jdbc4.jar” (1). Open Matlab command window and type
edit classpath.txt
Add the full path (quoted above) at the end of the file. In my case I added “$matlabroot/java/jar/postgresql-9.2-1002.jdbc4.jar” (2). Then close and start Matlab and verify the driver is loaded with typing in command window
javaclasspath
You should see the added driver path (1) above. Matlab supports connecting with and without SSL. Here are example strings to use in either cases:
JDBC Driver: org.postgresql.Driver
Database URL:jdbc:postgresql://localhost:5432/database_name
JDBC Driver: org.postgresql.Driver
Database URL: jdbc:postgresql:servername:database_name:ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory&
Querying connection
Connection string should look like
conn = database(database_name, database_user, user_password, 'Vendor', 'PostGreSQL');
Here is example to query “database_table” and show the data for the first row:
conn = database('database_name', 'database_user', 'user_password', 'org.postgresql.Driver', 'jdbc:postgresql://123.123.123:5432/database_name')
curs = exec(conn, ['SELECT * FROM "database_table";']);
row = fetch(curs, 1);
row.Data
Used arguments are self explaining and should be straightforward to follow.