Deploy PostgreSQL Native Interface Database Application with MATLAB Compiler
This example shows how to write a script to analyze data stored in a PostgreSQL database using the PostgreSQL native interface, and deploy the script as a standalone application. Write code that connects to a database using the PostgreSQL native interface, imports data from the database into MATLAB®, analyzes the data, and closes the database connection. Then, you can deploy the code by compiling it as a standalone application using the Standalone Application Compiler (MATLAB Compiler) app, and then running the application on other machines.
You must have administrator privileges on each machine where you plan to run the standalone application.
Create Function in MATLAB
Create a MATLAB script named importPostgreSQLNative.m
and save it in a file location of your choice. The script contains the importPostgreSQLNative
function, which returns the maximum product number from the data in the productTable
database table. The function connects to a PostgreSQL database and imports all data from productTable
. Then, the function calculates the maximum product number.
You must use the syntax with name-value arguments when you connect to the database using the postgresql
function.
type importPostgreSQLNative.m
function maxProdNum = importPostgreSQLNative % IMPORTPOSTGRESQLNATIVE The importPostgreSQLNative function connects to a % PostgreSQL database using the PostgreSQL native interface, imports data % from the database into MATLAB®, performs a simple data analysis, and % closes the database connection. The database contains a table named % |productTable|. %% % Connect to the database by using name-value pair arguments of the % |postgresql| function to specify a connection to a PostgreSQL database. % For example, this code assumes that you are using the user name % |username|, password |pwd|, database |dbname|, database server |sname|, % and port number |5432|. conn = postgresql("username","pwd", ... "DatabaseName","dbname", ... "Server","sname", ... "PortNumber",5432); %% % Import data from the |productTable| database table. tablename = "productTable"; data = sqlread(conn,tablename); %% % Determine the highest product number among products. prodNums = data.productnumber; maxProdNum = max(prodNums); %% % Close the database connection. close(conn) end
Create Standalone Application Using Application Compiler App
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow to open the apps gallery. Under Application Deployment, click Standalone Application Compiler.
After you open the app, the Create Compiler Task dialog box prompts you to add a compiler task to a new or an existing MATLAB® project. For this example, select Start a new project and create a compiler task and create a new project in your working folder. For more information on creating and using MATLAB® projects, see Create Projects.
A new compiler task named StandaloneDesktopApp1 opens in the Editor. You can compile code for other deployment targets by opening the Compiler Task Manager app or going to the Manage Tasks tab and creating a new compiler task.
For this example, in the Main File section of the compiler task, click Add Main File and select importPostgreSQLNative.m
. In the Project panel, the file now has the labels Design and Main Function. To set up the rest of the standalone application, see Specify Build Options (MATLAB Compiler).
See Also
postgresql
| close
| sqlread
Topics
- Create Functions in Files
- Create Standalone Application from MATLAB (MATLAB Compiler)
- Import Data from PostgreSQL Database Table
- Deploy Applications and MATLAB Runtime on Network Drives (MATLAB Compiler)
- Testing Failures (MATLAB Compiler)