Tuesday, November 18, 2014

PostgresQL Database connection with MATLAB

Configuration Steps

Steps:

1. Download and copy JDBC driver for Postgres version from this link
http://jdbc.postgresql.org/download.html

2. Follow this link to edit JDBC driver path to Matlab java class path.
http://in.mathworks.com/help/database/ug/postgresql-jdbc-windows.html

3. Follow above link to complete the connection.


Matlab Data Retrieval from PostgresQL 

1. Connect to database

DataBase =  database('hpc','postgres','postgres','Vendor','PostgreSQL')

2. Conver num to string for comparison

startdate= num2str('2014-11-02');
endddate=num2str('2014-11-08');

3. SQL query and execute the query

sqlquery = ['select temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8 from racktemp where nodeid = 7 and systemtime BETWEEN (''',startdate,''') and (''',endddate,''')'];
curs = exec(DataBase,sqlquery);

4. Fetch the data and store in an array

curs = fetch(curs);
Data = curs.Data;
Data(15,2)

5. Close connection

close(DataBase);

6. Use integer variable inside Postgres query in Matlab

are = 10
sqlquery = ['select loadavg1min from punecpuload where sno =' num2str(are)]

7. Use string variable inside Postgres query in Matlab

nodeL = num2str('r4-c1-n1');
sqlquery = ['select count(*) FROM consotabletemp where node_id =(''',nodeL,''')'];