%% Connect to PostgreSQL Database Using PostgreSQL Native Interface and Additional Options
% Create a PostgreSQL native interface connection to a PostgreSQL database using name-value pair arguments. Then, import data from the database into MATLAB® and perform simple data analysis. Close the database connection.
% This example assumes that you are connecting to a PostgreSQL database version 9.405 using the libpq driver version 10.12.
%
% Connect to the database using the user name and password shown. Specify the database server name dbtb00, database name toystore_doc, and port number 5432 by setting the corresponding name-value pair arguments.

username = "dbdev";
password = "matlab";

conn = postgresql(username,password,'Server',"dbtb00", ...
    'DatabaseName',"toystore_doc",'PortNumber',5432)

% The property sections of the connection object are:
% Database Properties — Information about the database configuration
% Catalog and Schema Information — Names of catalogs and schemas in the database
% Database and Driver Information — Names and versions of the database and driver
%
% Import all data from the table inventoryTable into MATLAB using the sqlread function. Display the first three rows of data.

tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data,3)

%
% Determine the highest product quantity from the table.

max(data.quantity)

%
% Close the database connection conn.

close(conn)

% Copyright 2020 The MathWorks, Inc.