This tutorial describes how to connect to PostgreSQL using the TPgConnection component.
This tutorial assumes that you have installed PgDAC and run the database server and the IDE. You need to know the server address, the port number (if you use a port other than the default port 5432
), the database name, the schema, and the username and password. To connect at runtime, add the PgAccess unit to the uses
clause for Delphi or include the PgAccess.hpp
header file for C++ Builder.
To establish a connection to the server, set up the properties of the TPgConnection component: Server, Port, Database, Schema, Username, and Password. You can also specify all connection parameters in the ConnectString property.
The following assumes that you have already created or opened an existing form in the IDE. At design-time, you can set up a TPgConnection
object in the TPgConnection Editor or Object Inspector.
TPgConnection
component in the PgDAC
category on the Tool Palette.
TPgConnection
in this unit, it will be named PgConnection1
.
Using TPgConnection Editor
PgConnection1
object.Server
edit box.5432
, specify it in the Port
edit box.postgres
by default) in the Username
edit box.postgres
by default) in the Password
edit box.Database
edit box.public
by default) in the Schema
edit box.Using Object Inspector
PgConnection1
object on the form.Database
property to the database name.Password
property to the password (postgres
by default).5432
, set the Port
property to the port.Schema
property to the database schema (public
by default).Server
property to the DNS name or IP address of the PostgreSQL server.Username
property to the username (postgres
by default).The same connection parameters at runtime are set up as follows:
Delphi
var
PgConnection1: TPgConnection;
begin
PgConnection1 := TPgConnection.Create(nil);
try
// adds connection parameters
PgConnection1.Server := 'server';
PgConnection1.Database := 'database';
PgConnection1.Username := 'username';
PgConnection1.Password := 'password';
PgConnection1.Port := 5432;
// disables a login prompt
PgConnection1.LoginPrompt := False;
// opens a connection
PgConnection1.Open;
finally
PgConnection1.Free;
end;
end;
C++ Builder
TPgConnection* PgConnection1 = new TPgConnection(NULL);
try {
// adds connection parameters
PgConnection1->Server = "server";
PgConnection1->Database = "database";
PgConnection1->Username = "username";
PgConnection1->Password = "password";
PgConnection1->Port = 5432;
// disables a login prompt
PgConnection1->LoginPrompt = False;
// opens a connection
PgConnection1->Open();
}
__finally {
PgConnection1->Free();
}
To open a connection at run-time, call the Open
method:
Delphi
PgConnection1.Open;
C++Builder
PgConnection1->Open;
Another way to open a connection at runtime is to set the Connected property to True
:
Delphi
PgConnection1.Connected := True;
C++ Builder
PgConnection1->Connected = True;
You can also set up the Connected
property at design-time in the Object Inspector.
You can modify a connection by changing properties of the TPgConnection
object. Note that while some of the object's properties can be altered without changing the state of a connection, in most cases, a connection is closed when a new value is assigned to the property. For example, if you change the value of the Server
property, a connection is closed immediately and you need to reopen it manually.
To close a connection, call the Close
method or set the Connected
property to False
:
Delphi
PgConnection1.Close;
or:
PgConnection1.Connected := False;
C++ Builder
PgConnection1->Close;
or:
PgConnection1->Connected = False;
PgDAC offers a wide set of features to achieve better performance, balance network load, and enable additional capabilities, for example: