dotConnect for PostgreSQL Documentation
In This Topic
    Logging Onto The Server
    In This Topic

    This tutorial describes how to connect to PostgreSQL server.

    In this tutorial:


    In order to connect to PostgreSQL server you need the server itself running, dotConnect for PostgreSQL installed and IDE running. You have to know host name or IP address of the host where the PostgreSQL server is located, port, login and password.

    Note that if you do not use design-time (specifically, if you do not place the PgSqlConnection component from the toolbox on a form designer), you have to embed licensing information manually. This is described in the Licensing topic.

    General Information

    To establish a connection to server you have to provide some connection parameters to dotConnect for PostgreSQL. This information is used by the PgSqlConnection component to find the server and login with credentials of your account. The parameters are represented as connection string. You can compose the connection string manually or have dotConnect for PostgreSQL construct it for you.

    Creating PgSqlConnection

    Design-time creation

    Note that design-time connection creation, described here, is applicable only to Windows Forms applications for Full .NET Framework.

    The following assumes that you have IDE running, and you are currently focused on a form designer.

    1. Open Toolbox and find PgSqlConnection component in dotConnect for PostgreSQL Data tab.

    2. Double-click the component. Note that new object appears on the designer underneath the form. If this is first time you create PgSqlConnection in this application, it is named pgSqlConnection1.

    3. Click on the pgSqlConnection1 object and press F4 to focus on object's properties.

    4. In the Host property provide host name or IP address of the host where the PostgreSQL server is located.

    5. In the Port property specify the port that the server listens to (usually this is 5432).

    6. In the UserId property specify your login. For example, postgres.

    7. In the Password property specify your password. For example, postgres.

    8. Note that as you assign values to these properties the ConnectionString property is automatically updated to reflect your settings. Now it contains something like User Id=postgres;Password=postgres;Host=db;.

    Run-time creation

    Same operations performed in runtime look as follows (note that you have to add references to Devart.Data.PostgreSql.dll and Devart.Data.dll assemblies for Full .NET Framework or install the Devart.Data.PostgreSql NuGet package for .NET Core):

    using Devart.Data.PostgreSql;
    PgSqlConnection pgSqlConnection1 = new PgSqlConnection();
    pgSqlConnection1.Host = "db";
    pgSqlConnection1.Port = 5432;
    pgSqlConnection1.UserId = "postgres";
    pgSqlConnection1.Password = "postgres";
    Imports Devart.Data.PostgreSql
    Dim pgSqlConnection1 As PgSqlConnection = New PgSqlConnection()
    pgSqlConnection1.Host = "db"
    pgSqlConnection1.Port = 5432
    pgSqlConnection1.UserId = "postgres"
    pgSqlConnection1.Password = "postgres"

    You can do this all in single assignment. It actually does not matter whether connection string is assigned directly or composed with particular properties. After you assign a value to ConnectionString property all other properties are populated with parsed values. So you can choose what is more convenient for you.

    pgSqlConnection1.ConnectionString = "User Id=postgres;Password=postgres;Host=db;";
    pgSqlConnection1.ConnectionString = "User Id=postgres;Password=postgres;Host=db;"

    Using connection string builder

    If you decide to setup a connection by assigning values to several properties, consider using the PgSqlConnectionStringBuilder class. It has all of the possible connection settings exposed as properties, thus allowing you to customize the connection at full extent. The following example demonstrates how to compose a more complex connection string:

    PgSqlConnectionStringBuilder pgCSB = new PgSqlConnectionStringBuilder();
    pgCSB.Host = "";
    pgCSB.Port = 5432;
    pgCSB.UserId = "postgres";
    pgCSB.Password = "postgres";
    pgCSB.MaxPoolSize = 150;
    pgCSB.ConnectionTimeout = 30;
    PgSqlConnection myConnection = new PgSqlConnection(pgCSB.ConnectionString);
    Dim pgCSB As PgSqlConnectionStringBuilder = New PgSqlConnectionStringBuilder
    pgCSB.Host = ""
    pgCSB.Port = 5432
    pgCSB.UserId = "postgres"
    pgCSB.Password = "postgres"
    pgCSB.MaxPoolSize = 150
    pgCSB.ConnectionTimeout = 30
    Dim myConnection As PgSqlConnection = New PgSqlConnection(pgCSB.ConnectionString)

    Note that in this example we used PgSqlConnection constructor that accepts connection string as argument.

    For the information on arguments allowed in the connection string, refer to the description of the PgSqlConnection.ConnectionString property.

    Opening Connection

    Opening a connection is as simple as that:


    Of course, pgSqlConnection1 must have a valid connection string assigned earlier. When you call Open, dotConnect for PostgreSQL tries to find the host and connect to server. If any problem occurs it raises an exception with brief explanation on what is wrong. If no problem is encountered dotConnect for PostgreSQL tries to establish the connection during ConnectionTimeout interval. Finally, when connection is established, the Open method returns and State property is changed to Open.

    In design-time you can connect to server in few steps:

    1. Right-click on pgSqlConnection1 object in designer.

    2. Select Connect from the popup menu.

    3. In the dialog window provide necessary logon information.

    4. Click the Connect button to establish connection.

    Or you can simply change the State property to Open in the Properties window to establish connection using the current connection string.

    Closing Connection

    To close a connection call its Close method, or set its State property to Closed.

    The following example summarizes aforementioned information and shows how to create, setup, open, use and then close the connection.

    PgSqlConnection myConn = new PgSqlConnection();
    myConn.ConnectionString = "User Id=postgres;Password=postgres;Host=db;Port=5432;";
    Dim myConn As PgSqlConnection = New PgSqlConnection()
    myConn.ConnectionString = "User Id=postgres;Password=postgres;Host=db;Port=5432;"

    The sample code connects to a server, shows its version and then closes the connection. This actually is rare usage, because in real applications connections are used by other objects like PgSqlCommand, PgSqlDataTable and others. For more information on this, please see the corresponding tutorials or the reference information.

    Modifying Connection

    You can modify connection by changing properties of PgSqlConnection object. Keep in mind that while some of the properties can be altered freely, most of them close connection when new value is assigned. For example, if you change Host property, it gets closed immediately, and you have to reopen it manually.

    Additional Information

    dotConnect for PostgreSQL has wide set of features you can take advantage of. The following list enumerates some of them so you can explore the advanced techniques to achieve better performance, balance network load or enable additional capabilities.

    See Also

    Getting Started  | PgSqlConnection Class