testConnection
Syntax
Description
tests the PostgreSQL native interface database connection specified by the status
= testConnection(opts
,username
,password
)SQLConnectionOptions
object opts
, a user name, and a
password.
Examples
Create PostgreSQL Native Interface Data Source
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 sourceVendor
— Database vendor nameDatabaseName
— Name of the databaseServer
— Name of the database serverPortNumber
— 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.
Retrieve Message for PostgreSQL Native Interface Database Connection Test
Create and configure a PostgreSQL native interface data source to a PostgreSQL database. Test the database connection to the PostgreSQL native interface data source and retrieve the error message.
Create a PostgreSQL native interface data source for a PostgreSQL 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 sourceVendor
— Database vendor nameDatabaseName
— Name of the databaseServer
— Name of the database serverPortNumber
— 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 using an incorrect user name and password. The
testConnection
function returns the logical 0
,
which indicates the database connection fails. Retrieve and display the error message
for the failed connection.
username = "wronguser"; password = "wrongpassword"; [status,message] = testConnection(opts,username,password)
status = logical
0
message =
'Driver Error: FATAL: password authentication failed for user "wronguser"
FATAL: password authentication failed for user "wronguser"
'
Input Arguments
opts
— Database connection options
SQLConnectionOptions
object
Database connection options, specified as an SQLConnectionOptions
object.
username
— User name
character vector | string scalar
User 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
— Password
character vector | string scalar
Password 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
Output Arguments
status
— Connection status
logical
Connection status, returned as a logical true
if the connection
test passes or false
if the connection test fails.
message
— Error message
character vector
Error message, returned as a character vector. If the connection test passes, then the error message is an empty character vector. Otherwise, the error message contains text describing the failed database connection.
Version History
Introduced in R2020b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)