Create PostgreSQL native interface database connection
creates a PostgreSQL native interface database connection using the specified data source,
user name, and password. conn
= postgresql(datasource
,username
,password
)conn
is a connection
object.
creates a PostgreSQL native interface database connection using the specified user name and
password, with additional options specified by one or more name-value pair arguments. For
example, conn
= postgresql(username
,password
,Name,Value
)"Server","dbtb00"
specifies the database server name as
dbtb00
.
Create a PostgreSQL native interface connection to a PostgreSQL database. 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 data source name, user name, and password.
datasource = "PostgreSQLDataSource"; username = "dbdev"; password = "matlab"; conn = postgresql(datasource,username,password)
conn = connection with properties: DataSource: "PostgreSQLDataSource" UserName: "dbdev" Database Properties: AutoCommit: "on" LoginTimeout: 0 MaxDatabaseConnections: 100 Catalog and Schema Information: DefaultCatalog: "toystore_doc" Catalogs: "toystore_doc" Schemas: ["pg_toast", "pg_temp_1", "pg_toast_temp_1" ... and 3 more] Database and Driver Information: DatabaseProductName: "PostgreSQL" DatabaseProductVersion: "9.405" DriverName: "libpq" DriverVersion: "10.12"
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)
ans=3×4 table
productnumber quantity price inventorydate
_____________ ________ _____ _____________________
1 1700 14.5 "2014-09-23 09:38:34"
2 1200 9 "2014-07-08 22:50:45"
3 356 17 "2014-05-14 07:14:28"
Determine the highest product quantity from the table.
max(data.quantity)
ans = 9000
Close the database connection conn
.
close(conn)
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)
conn = connection with properties: Database: "toystore_doc" UserName: "dbdev" Database Properties: AutoCommit: "on" LoginTimeout: 0 MaxDatabaseConnections: 100 Catalog and Schema Information: DefaultCatalog: "toystore_doc" Catalogs: "toystore_doc" Schemas: ["pg_toast", "pg_temp_1", "pg_toast_temp_1" ... and 3 more] Database and Driver Information: DatabaseProductName: "PostgreSQL" DatabaseProductVersion: "9.405" DriverName: "libpq" DriverVersion: "10.12"
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)
ans=3×4 table
productnumber quantity price inventorydate
_____________ ________ _____ _____________________
1 1700 14.5 "2014-09-23 09:38:34"
2 1200 9 "2014-07-08 22:50:45"
3 356 17 "2014-05-14 07:14:28"
Determine the highest product quantity from the table.
max(data.quantity)
ans = 9000
Close the database connection conn
.
close(conn)
datasource
— Data source nameData source name, specified as a character vector or string scalar. Specify the name of an existing data source.
Example: "myDataSource"
Data Types: char
| string
username
— User nameUser name required to access the database, specified as a character vector or string scalar.
If no user name is required, specify an empty value ""
.
Data Types: char
| string
password
— PasswordPassword required to access the database, specified as a character vector or string scalar. If
no password is required, specify an empty value ""
.
Data Types: char
| string
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
conn =
postgresql(username,password,"Server","dbtb00","PortNumber",5432,"DatabaseName","toystore_doc")
creates a PostgreSQL native interface database connection using the database server
dbtb00
, port number 5432
, and database name
toystore_doc
.'Server'
— Database server name"localhost"
(default) | string scalar | character vectorDatabase server name or address, specified as the comma-separated pair consisting
of 'Server'
and a string scalar or character vector.
Example: "dbtb00"
Data Types: char
| string
'PortNumber'
— Port number5432
(default) | numeric scalarPort number, specified as the comma-separated pair consisting of
'PortNumber'
and a numeric scalar.
Example: 5432
Data Types: double
'DatabaseName'
— Database name""
(default) | string scalar | character vectorDatabase name, specified as the comma-separated pair consisting of
'DatabaseName'
and a string scalar or character vector. If you do
not specify a database name, the postgresql
function connects to
the default database on the database server.
Example: "toystore_doc"
Data Types: char
| string
You have a modified version of this example. Do you want to open this example with your edits?