Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a postgresql database from matlab with without matlabs database toolbox?

I tried already to use pgmex. Unfortunately it doesn't work with libpq5 (matlab immediately crashes).

like image 723
Philipp der Rautenberg Avatar asked Apr 23 '10 11:04

Philipp der Rautenberg


1 Answers

To connect to postgres from matlab without the database toolbox do something similar to:

% Add jar file to classpath (ensure it is present in your current dir)
javaclasspath('postgresql-9.0-801.jdbc4.jar');

% Username and password you chose when installing postgres
props=java.util.Properties;
props.setProperty('user', '<your_postgres_username>');
props.setProperty('password', '<your_postgres_password>');

% Create the database connection (port 5432 is the default postgres chooses
% on installation)
driver=org.postgresql.Driver;
url = 'jdbc:postgresql://<yourhost>:<yourport>\<yourdb>';
conn=driver.connect(url, props);

% A test query
sql='select * from <table>'; % Gets all records
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();

% Read the results into an array of result structs
count=0;
result=struct;
while rs.next()
    count=count+1;
    result(count).var1=char(rs.getString(2));
    result(count).var2=char(rs.getString(3));
    ...
end
like image 185
user574057 Avatar answered Sep 24 '22 12:09

user574057