Main Content

SQLConnectionOptions

Define PostgreSQL native interface database connection options

Description

Create database connection options for a PostgreSQL native interface connection.

First, create an SQLConnectionOptions object, set the connection options, test the connection, and save the data source. Then, create a PostgreSQL native interface connection using the saved data source. The connection options include the options required to make a database connection. You can also define additional connection options for a specific database driver.

Creation

Create an SQLConnectionOptions object using the databaseConnectionOptions function.

Properties

expand all

Data source name, specified as a string scalar. You can use the data source name in the postgresql function to create a database connection for the PostgreSQL native interface.

Example: "PostgreSQLDataSource"

Data Types: string

This property is read-only.

Database vendor, specified as a string scalar. Specify this property using the vendor input argument in the databaseConnectionOptions function. After the SQLConnectionOptions object exists, you cannot set this property to another value.

Example: "PostgreSQL"

Data Types: string

Database name, specified as a string scalar. Set this property using the setoptions function.

Example: "toystore_doc"

Data Types: string

Database server name or address, specified as a string scalar. Set this property using the setoptions function.

Data Types: string

Server port number where the server is listening, specified as a numeric scalar. Set this property using the setoptions function.

Data Types: double

Object Functions

rmoptionsRemove PostgreSQL native interface connection options
saveAsDataSourceSave PostgreSQL native interface data source
setoptionsSet PostgreSQL native interface connection options
resetReset PostgreSQL native interface connection options to defaults
testConnectionTest PostgreSQL native interface database connection

Examples

collapse all

Create, configure, test, and save a PostgreSQL native interface data source for a PostgreSQL database.

Create a PostgreSQL native interface data source for a PostgreSQL native interface database connection.

vendor = "PostgreSQL";
opts = databaseConnectionOptions("native",vendor)
opts = 
  SQLConnectionOptions with properties:

              DataSourceName: ""
                      Vendor: "PostgreSQL"

                DatabaseName: ""
                      Server: "localhost"
                  PortNumber: 5432

opts is an SQLConnectionOptions object with these properties:

  • DataSourceName — Name of the data source

  • Vendor — Database vendor name

  • DatabaseName — Name of the database

  • Server — Name of the database server

  • PortNumber — Port number

Configure the data source by setting the database connection options for the data source PostgreSQLDataSource, database name toystore_doc, database server dbtb00, and port number 5432.

opts = setoptions(opts, ...
    'DataSourceName',"PostgreSQLDataSource", ...
    'DatabaseName',"toystore_doc",'Server',"dbtb00", ...
    'PortNumber',5432)
opts = 
  SQLConnectionOptions with properties:

              DataSourceName: "PostgreSQLDataSource"
                      Vendor: "PostgreSQL"

                DatabaseName: "toystore_doc"
                      Server: "dbtb00"
                  PortNumber: 5432

The setoptions function sets the DataSourceName, DatabaseName, Server, and PortNumber properties in the SQLConnectionOptions object.

Test the database connection with a user name and password. The testConnection function returns the logical 1, which indicates the database connection is successful.

username = "dbdev";
password = "matlab";
status = testConnection(opts,username,password)
status = logical
   1

Save the configured data source.

saveAsDataSource(opts)

You can connect to the new data source using the postgresql function or the Database Explorer app.

Introduced in R2020b