Point
It is recommended to use a connection service file to specify connection destinations. In the connection service file, a name (service name) is defined as a set, comprising information such as connection destination information and various types of tuning information set for connections. By using the service name defined in the connection service file when connecting to databases, it is no longer necessary to modify applications when the connection information changes.
Refer to "The Connection Service File" in "Client Interfaces" in the PostgreSQL Documentation for information.
If using a connection service file, perform either of the procedures below:
Set the service name as a string literal or host variable, as follows:
tcp:postgresql://?service=my_service
Set the service name in the environment variable PGSERVICE, and use CONNECT TO DEFAULT
Use the CONNECT statement shown below to create a connection to the database server.
Format
EXEC SQL CONNECT TO target [AS connection-name] [USER user-name];
Write in one of the following formats:
dbname@host:port
tcp:postgresql://host:port/dbname[?options]
unix:postgresql://host[:port][/dbname][?options]
(Definition method when using the UNIX domain socket)
SQL string literal containing one of the above formats
Reference to a character variable containing one of the above formats
DEFAULT
Write in one of the following formats:
username
username/password
username IDENTIFIED BY password
username USING password
Description of the arguments
Argument | Description |
---|---|
dbname | Specify the database name. |
host | Specify the host name for the connection destination. |
port | Specify the port number for the database server. The default is "26500". |
connection-name | Specify connection names to identify connections when multiple connections are to be processed within a single program. |
username | Specify the user that will connect with the database. If this is omitted, the name used will be that of the user on the operating system that is executing the application. |
password | Specify a password when authentication is required. |
options | Specify the following parameter when specifying a time for timeout. Connect parameters with & when specifying more than one. The following shows the values specified for each parameter.
|
Code examples for applications
EXEC SQL CONNECT TO tcp:postgresql://sv1:26500/mydb?connect_timeout=20&keepalives_idle=20&keepalives_interval=5&keepalives_count=2&keepalives=1 USER myuser/myuser01;