.NET PostgreSQL Connection Strings
This documentation article details how to construct a connection string for your .NET application to connect to a PostgreSQL database using dotConnect for PostgreSQL. We'll delve into the essential connection string parameters and explore some advanced options for customizing your database connection.
PostgreSQL connection strings
These connection strings are used to establish a general connection to a PostgreSQL database without using any intermediate software.
using Devart.Data.PostgreSql; ... PgSqlConnection connection = new PgSqlConnection(); connection.Host = "127.0.0.1"; connection.Port = 5432; connection.UserId = "postgres"; connection.Password = "postgres"; connection.Database = "test"; connection.Schema = "public";
Connection properties table
Property | Meaning |
---|---|
Host | Specifies the hostname or IP address of the PostgreSQL server. |
Port | States the port number on which the PostgreSQL server is listening. |
UserId | Indicates the user ID used to authenticate with PostgreSQL. |
Password | Defines the password for the user ID. |
Database | Specifies the default database to use after connecting. |
Schema | The PostgreSQL schema to use. |
License Key | Specify your license key in this parameter. This is required only when using .NET Standard compatible assemblies. |
SSL/TLS connection strings
SSL/TLS connection strings are used to configure a secure connection to a PostgreSQL database with the help of SSL/TLS protocols.
The sample code below illustrates the establishment of the SSL connection.
PgSqlConnection connection = new PgSqlConnection("Host=127.0.0.1;Port=5432;UserId=postgres;Password=postgres;Database=test;"); connection.SslOptions.CACert = "E:\\Test\\root.crt"; connection.SslOptions.Cert = "E:\\Test\\client.crt"; connection.SslOptions.Key = "E:\\Test\\client.key"; connection.SslOptions.SslMode = SslMode.Require; connection.Open();
SSL options properties
Property | Meaning |
---|---|
CACert | Specifies the path to the SSL CA certificate file. |
Cert | Indicates the location of the SSL client certificate file. |
Key | States the path to the SSL client key file. |
SslMode | Defines the mode of the SSL operation to use. |
SSH connection strings
A benefit of SSH tunneling is that it allows you to connect to the PostgreSQL server from behind a firewall when the server port is blocked. The PostgreSQL server does not need to be tuned for this type of connection and functions as usual. To connect to the server, a client must first be authorized on the SSH server.
PgSqlConnection.SshOptions property points to an object that keeps all information necessary to connect to the SSH server. Alternatively, specify the corresponding parameters in the connection string.
To set up an SSH connection, adjust the corresponding SSH options as described below:
Password authentication
PgSqlConnection connection = new PgSqlConnection("Host=127.0.0.1;Port=5432;UserId=postgres;Database=test;"); connection.SshOptions.AuthenticationType = SshAuthenticationType.Password; connection.SshOptions.User = "sshUser"; connection.SshOptions.Host = "sshServer"; connection.SshOptions.Password = "sshPassword"; PgSqlCommand command = new PgSqlCommand("select count(*) from dept", connection); connection.Open(); Int64 count = Convert.ToInt64(command.ExecuteScalar()); Console.WriteLine(count); connection.Close();
Public key authentication
PgSqlConnection connection = new PgSqlConnection("Host=127.0.0.1;Port=5432;UserId=postgres;Database=test;"); connection.SshOptions.AuthenticationType = SshAuthenticationType.PublicKey; connection.SshOptions.User = "sshUser"; connection.SshOptions.Host = "sshServer"; connection.SshOptions.PrivateKey = "E:\\WORK\\client.key"; PgSqlCommand command = new PgSqlCommand("select count(*) from dept", connection); connection.Open(); Int64 count = Convert.ToInt64(command.ExecuteScalar()); Console.WriteLine(count); connection.Close();
Keyboard-interactive authentication
PgSqlConnection connection = new PgSqlConnection("Host=127.0.0.1;UserId=postgres;Database=test;"); connection.Protocol = PgSqlProtocol.Ssh; // sets ssh options connection.SshOptions.Host = "ssh_host"; connection.SshOptions.Port = 22; connection.SshOptions.User = "ssh_user"; connection.SshOptions.AuthenticationType = SshAuthenticationType.KeyboardInteractive; // Associate the AuthenticationPrompt event with your event handle connection.AuthenticationPrompt += new PgSqlAuthenticationPromptHandler(connection_AuthenticationPrompt); connection.Open(); Console.WriteLine(connection.State); connection.Close(); Console.ReadKey(); ... static void connection_AuthenticationPrompt(object sender, PgSqlAuthenticationPrompEventArgs e) { foreach (string prompt in e.Prompts) { if (prompt.Contains("Password")) e.Responses[0] = "testPassword"; else if (prompt.Contains("UserId")) e.Responses[1] = "testUserId"; } }
SSH options properties
Property | Connection string parameter | Meaning |
---|---|---|
SshOptions.AuthenticationType | SSH Authentication Type | Client authentication methods. |
SshOptions.CipherList | SSH Cipher List | List of ciphers that the client agrees to use, by colons. |
SshOptions.Host | SSH Host | Name or IP address of the SSH server. |
SshOptions.Passphrase | SSH Passphrase | Passphrase for the client key. |
SshOptions.Password | SSH Password | User password for the SSH server. |
SshOptions.Port | SSH Port | Number of the port on the SSH server to connect. |
SshOptions.PrivateKey | SSH Private Key | Location of the private key to use. |
SshOptions.User | SSH User | User ID for the SSH server. |
Locations of the private key can be specified in three ways:
-
As a file in the system:
file://C:\Temp\client.key
-
As an item of the certificate store:
storage://Name.Id
(examples RSA.Client.key or DSA.Client.key) -
As compiled resource:
resource://client.key
-
Loaded to memory, using
MemCryptStorage class: memory://key_id
The PostgreSQL server address that you specify in the connection string is the address for the SSH server to refer to. For instance, if both servers are running on the same machine you have to set host=localhost
in the connection string.
The property SshOptions.CipherList contains the list of the ciphers that the client agrees to use, separated by colons. By default, it is empty, which means that the client agrees to use any of the available ciphers. The appropriate values for the CipherList property are listed below, highlighted in bold.
dotConnect for PostgreSQL supports two modes of block ciphering: Cipher-block chaining (CBC) and Counter (CTR). The following ciphers are available for SSH connections in the CBC mode:
- 3DES or 3DES(168) is the Triple Data Encryption Algorithm. The key size is 168 bits
- Blowfish is the symmetric-key block cipher, designed in 1993 by Bruce Schneier. The key size is 128 bits
- AES(128) is the Advanced Encryption Standard. The key size is 128 bits
- AES(192) is the Advanced Encryption Standard. The key size is 192 bits
- AES or AES(256) is the Advanced Encryption Standard. The key size is 256 bits
In the CTR mode, the AES ciphers are used.
- AES(128)-CTR is the Advanced Encryption Standard. The key size is 128 bits
- AES(192)-CTR is the Advanced Encryption Standard. The key size is 192 bits
- AES-CTR or AES(256)-CTR is the Advanced Encryption Standard. The key size is 256 bits
Proxy connection strings
Proxy connection strings are used to connect to a PostgreSQL database through a proxy server, which is useful for environments with strict network controls.
PgSqlConnection connection = new PgSqlConnection(); connection.ConnectionString = "Host=127.0.0.1;UserId=postgres;Password=postgres;Database=test;"; connection.ProxyOptions.Host = "10.0.0.1"; connection.ProxyOptions.Port = 808; connection.ProxyOptions.User = "ProxyUser"; connection.ProxyOptions.Password = "ProxyPassword"; connection.Open();
Proxy options properties
Property | Connection string parameter | Description |
---|---|---|
ProxyOptions.Host | Proxy Host | Specifies the proxy server hostname or IP address. |
ProxyOptions.Port | Proxy Port | Defines the proxy server port number. |
ProxyOptions.User | Proxy User | States the proxy user name for authentication. |
ProxyOptions.Password | Proxy Password | Indicates the proxy password for authentication. |
Unicode property connection strings
Unicode property connection strings ensure that all data transferred between the application and a PostgreSQL database is in the Unicode format.
PgSqlConnection connection = new PgSqlConnection(); connection.ConnectionString = "Host=127.0.0.1;UserId=postgres;Password=postgres;Database=test;Unicode=true;"; connection.Open();
PgSqlConnection connection string parameter
Parameter | Meaning |
---|---|
Unicode | Enables or disables the Unicode property. When set to true, it ensures all data transferred is in the Unicode format. |
Pooling connection string options
Parameter | Meaning |
---|---|
Pooling | If true, by default, the PgSqlConnection object is fetched from the appropriate pool or is created and added to the appropriate pool. |
Max Pool Size | The maximum number of connections allowed in the pool. Setting the Max Pool Size value of the ConnectionString can affect performance. The default value is 100. In the case of forgotten open transactions, the number of connections can exceed the Max Pool Size. |
Min Pool Size | The minimum number of connections allowed in the pool. The default value is 0. |
Advanced connection string options
Parameter | Meaning |
---|---|
Validate Connection | The connection specifies whether to validate connections that are being obtained from the pool. |
Connection Lifetime | When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. The default value is 0. |
Protocol | The frontend/backend protocol version. Available values are 2 and 3. Set the parameter to 2 for protocol version 2.0 or to 3 for protocol version 3.0. Can be applied only for PostgreSQL server versions 7.4 or higher, for earlier versions of PostgreSQL this parameter must be explicitly set to 2. To execute several statements in the same query set protocol to version 2. The default value is 3. |
Run Once Command | The command is executed when the connection is first opened, but not when the connection is taken from the pool. |
Initialization Command | The command defines a database-specific command that should be executed immediately after establishing the connection. |
We use cookies to provide you with a better experience on the Devart website. You can read more about our use of cookies in our Cookies Policy.
Click OK to continue browsing the Devart site. Be aware you can disable cookies at any time.