PgDAC

Logging on to PostgreSQL

This tutorial describes how to connect to PostgreSQL.

Contents

  1. Requirements
  2. General information
  3. Creating Connection
  4. Opening connection
  5. Closing connection
  6. Modifying connection
  7. Additional information
  8. See Also

Requirements

In order to connect to PostgreSQL, you need the server itself running, PgDAC installed, and IDE running. Also, you need to know the server name (if the server is run on the remote computer), the port that the server listens to (if you use not the 5432 standard port), and the database name.

General information

To establish connection to the server, you have to provide some connection parameters to PgDAC. This information is used by the TPgConnection component to establish connection to the server. The parameters are represented by the properties of the TPgConnection component (Server, Port, Username, Password, Database, Schema).

Note: All these options can be specified at once using the ConnectString property.

Creating Connection

Design time creation

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

  1. Open the Tool Palette and find the TPgConnection component in the PgDAC category.
  2. Double-click on the component. Note that the new object appears on the form. If this is the first time you create TPgConnection in this application, it is named PgConnection1.

After you have done these steps, you should set up the newly created PgConnection1 component. You can do as follows:

Using TPgConnection Editor

  1. Double-click on the PgConnection1 object.
  2. In the Server edit box specify a DNS name or IP address of the computer, where PostgreSQL resides (for example, localhost). If not the 5432 standard port must be used, it can be specified in the Port edit box in the following format: Port (for example, 5432).
  3. Specify a login (for example, postgres in the Username edit box.
  4. Specify a password (for example, password) in the Password edit box. If a login does not have a password, leave the Password edit box blank.
  5. In the Database edit box specify the database name (for example, postgres). If Database is not specified, the postgres system database is used.
  6. In the Schema edit box specify the schema name (for example, public).

Using Object Inspector

  1. Click on the PgConnection1 object and press F11 to focus on object's properties.
  2. Set the Server property to a DNS name or IP address of the computer, where PostgreSQL resides(for example, localhost). If not the 5432 standard port must be used, it can be specified in the Port edit box in the following format: Port (for example, 5432).
  3. Specify a login (for example, postgres in the Username edit box.
  4. Specify a password (for example, password) in the Password edit box. If a login does not have a password, leave the Password edit box blank.
  5. In the Database edit box specify the database name (for example, postgres). If Database is not specified, the postgres system database is used.
  6. In the Schema edit box specify the schema name (for example, public).

Run time creation

The same operations performed in runtime look as follows:
[Delphi]


var
  con: TPgConnection;
begin
  con := TPgConnection.Create(nil);
  try
    con.Server := 'localhost';
    con.Port := 5432;      
    con.Username := 'postgres';
    con.Password := 'password';
    con.Database := 'postgres';
	con.Schema :='public';
    con.LoginPrompt := False; // to prevent showing of the connection dialog
    con.Open;
  finally
    con.Free;
  end;
end;

Note: To run this code, you have to add the PgAccess unit to the USES clause of your unit.


[C++Builder]


{
  TPgConnection* con = new TPgConnection(NULL);
  try
  {
    con->Server = "localhost";  
	con->Port = 5432;  
    con->Username = "postgres";
    con->Password = "password";
    con->Database = "postgres";
	con->Schema = "public";
    con->LoginPrompt = False; // to prevent showing of the connection dialog
    con->Open();
  }
  __finally
  {
    con->Free();
  }
}

Note: To run this code, you have to include the PgAccess.hpp header file to your unit.

And using the ConnectString property:


[Delphi]


var
  con: TPgConnection;
begin
  con := TPgConnection.Create(nil);
  try
    con.ConnectString := 'Data Source=localhost;Port=5432;Database=postgres;User ID=postgres;Password=password;Schema=public;Login Prompt=False';
    con.LoginPrompt := False; // to prevent showing of the connection dialog
    con.Open;
  finally
    con.Free;
  end;
end;

Note: To run this code, you have to add the PgAccess units to the USES clause of your unit.


[C++ Builder]


{
  TPgConnection* con = new TPgConnection(NULL);
  try
  {
    con->ConnectString = "Data Source=localhost;Port=5432;Database=postgres;User ID=postgres;Password=password;Schema=public;Login Prompt=False";
    con->LoginPrompt = False; // to prevent showing of the connection dialog
    con->Open();
  }
  __finally
  {
    con->Free();
  }
}

Note: To run this code, you have to include the PgAccess.hpp header file to your unit.

Opening connection

As you can see above, opening a connection at run-time is as simple as calling of the Open method:

[Delphi]


con.Open;

[C++ Builder]


con->Open();

Another way to open a connection at run-time is to set the Connected property to True:

[Delphi]


con.Connected := True;

[C++ Builder]


con->Connected = True;

This way can be used at design-time as well. Of course, PgConnection1 must have valid connection options assigned earlier. When you call Open, PgDAC tries to find the host and connect to the server. If any problem occurs, it raises an exception with brief explanation on what is wrong. If no problem is encountered, PgDAC tries to establish connection. Finally, when connection is established, the Open method returns and the Connected property is changed to True.

Closing connection

To close a connection, call its Close method, or set its Connected property to False:
[Delphi]


con.Close;

[C++ Builder]


con.Close();

or:

[Delphi]


con.Connected := False;

[C++ Builder]


con.Connected = False;

Modifying connection

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

Additional information

PgDAC has a 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

© 1997-2020 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback