I tried already to use pgmex. Unfortunately it doesn't work with libpq5 (matlab immediately crashes).
views:
146answers:
2Would MYSQL (additional link) work for you, at least as a starting point?
As a general solution, you can just use JDBC directly. Modern Matlabs all have a JVM embedded in them. Get the Postgresql JDBC driver JAR file on your Java CLASSPATH in Matlab and you can construct JDBC connection and statement objects. See "help javaclasspath".
There are a couple gotchas. Automatic registration of JDBC driver classes from JARs on the dynamic classpath in Matlab seems a little quirky, maybe because it uses a separate URL classloader and the core JDBC classes are in the system classloader. So you may need to explicitly construct instances of the JDBC driver class and pass them to the JDBC methods, instead of using the implicit driver construction that you see in all the JDBC tutorials. Also, there's performance overhead with each Java method call made from Matlab, which can become expensive if you're looping over a result set cursor in Matlab code. It's worthwhile to write a thin wrapper layer in Java that will wrap JDBC's iterative interface in a block-oriented Matlab-style interface, reading in the result sets and buffer them in arrays in Java, and passing the whole arrays back to Matlab.
You could use ODBC, too, but that requires writing MEX files linked against ODBC or working with ADO. More difficult and less portable.