Main Content

saveAsDataSource

Save PostgreSQL native interface data source

Description

example

saveAsDataSource(opts) saves the PostgreSQL native interface data source specified by the SQLConnectionOptions object opts.

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.

Input Arguments

collapse all

Database connection options, specified as an SQLConnectionOptions object.

Introduced in R2020b