PgDAC

Connecting to Heroku Postgres

Connecting to Heroku Postgres using PostgreSQL Data Access Components

Heroku is a cloud-based multilingual platform-as-a-service (PaaS) that is designed for hosting applications and web services, working with loaded applications, reducing the need for complex work with the server, and rapid scaling of applications. It also simplifies and speeds up the development cycle.

Requirements

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.

General Information

To establish a connection to your Heroku PostgresSQL database, 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.

To connect to the Heroku Postgres, you need to get the URL of your database. You may get it through the Heroku Dashboard. This method is easier if you are using the platform for the first time.

  1. Go to your application on Heroku you want to connect to.
  2. Switch to the Settings tab and press the Reveal Config Vars button. The list of configuration variables will appear.
  3. In the Key field, you will see the key value in the following format:
    < postgres: //< username >: < password >@< hostname/server >/< databasename >

Creating a Connection

Connecting at Design-Time

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.

  1. Find the TPgConnection component in the PgDAC category on the Tool Palette.
  2. Double-click the component. A new object will appear on the form. If this is the first object TPgConnection in this unit, it will be named PgConnection1.

Using TPgConnection Editor

  1. Double-click the PgConnection1 object.
  2. Specify the DNS name or IP address of the Heroku Postgres server in the Server edit box.
  3. If you use a port other than the default port 5432, specify it in the Port edit box.
  4. Specify the username (postgres by default) in the Username edit box.
  5. Specify the password (postgres by default) in the Password edit box.
  6. Specify the database name in the Database edit box.
  7. Specify the schema (public by default) in the Schema edit box.

Using Object Inspector

  1. Select the PgConnection1 object on the form.
  2. Set the Database property to the database name.
  3. Set the Password property to the password (postgres by default).
  4. If you use a port other than the default port 5432, set the Port property to the port.
  5. Set Schema property to the database schema (public by default).
  6. Set the Server property to the DNS name or IP address of the Heroku Postgres server.
  7. Set the Username property to the username (postgres by default).

Connecting at Runtime

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();
}

Now, all you have to do is establish a connection to Heroku Postgres. It is assumed that you have installed the Heroku Postgres Data Access Componnets on your computer.

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