Connecting to Heroku Postgres in a in a Private or Shield Space via PrivateLink | Heroku Dev Center (original) (raw)

English — 日本語に切り替える

Last updated February 07, 2025

Table of Contents

This article describes how to use AWS PrivateLink to create a secure connection between an AWS VPC and a Heroku Postgres database running in a Private Space or a Shield Private Space. This process involves three high-level steps:

  1. Creating an endpoint service on your Private or Shield Heroku Postgres database
  2. Creating an endpoint network interface in your AWS VPC
  3. Establishing the secure connection between the two endpoints

As part of setting up the connection, you can specify a list of approved accounts to limit access to your Heroku Postgres database from the VPC.

To use this feature, you must provision the Amazon VPC you create in a subnet that’s in the same region as your Heroku Postgres database. See Regions for more information.

Heroku Prerequisites

The following Heroku resources are required to set up a PrivateLink endpoint:

Provisioning the Heroku Endpoint

To install the CLI plugin, run the command:

$ heroku plugins:install @heroku-cli/plugin-data-privatelink

Step 2: Obtain Your AWS Account ID

You can obtain your AWS account ID with the AWS CLI:

$ aws sts get-caller-identity --output text --query 'Account'

123456789101

The example command returns an account ID of 123456789101.

You can also obtain your account ID from the Account page of your AWS account. The Account ID is shown in the Account Settings section:

The Account page of a sample AWS account

Create a PrivateLink endpoint using the following Heroku CLI command and substitute the values:

$ heroku data:privatelink:create POSTGRESQL_ADDON_NAME --aws-account-id ACCOUNT_ID --app APP_NAME

You can specify the --aws-account-id flag multiple times to include multiple accounts.

Here’s an example command with the output:

$ heroku data:privatelink:create postgresql-simply-12345 --aws-account-id 123456789101:user/abc.xyz --app privatelink-vpc-endpoint-demo
Creating privatelink... done

Service Name: Provisioning
Status:       Provisioning

The privatelink is now being provisioned for postgresql-simply-12345.
Run heroku data:privatelink:wait postgresql-simply-12345 -a APP to check the creation process.

New PrivateLink endpoints typically take 5–10 minutes to become available. You can track your progress with heroku data:privatelink:wait postgresql-simply-12345 --app APP_NAME.

Step 4: Obtain Your Endpoint’s Service Name

When the PrivateLink endpoint finishes provisioning, use the command to view its details:

$ heroku data:privatelink POSTGRESQL_ADDON_NAME --app APP_NAME

Replace POSTGRESQL_ADDON_NAME with the name of your Heroku Postgres database, and replace APP_NAME with your app’s name.

Here’s an example command with the output:

$ heroku data:privatelink postgresql-simply-12345 --app privatelink-vpc-endpoint-demo
=== privatelinks for postgresql-simply-12345
Service Name:         com.amazonaws.vpce.us-east-1.vpce-svc-0410a2e25933fe8ec
Status:               Operational

=== Allowed Accounts
ARN                                    Status
arn:aws:iam::123456789101:user/abc.xyz Active

Your privatelink is now operational.
You must now copy the Service Name and follow the rest of the steps listed in https://devcenter.heroku.com/articles/heroku-postgres-via-privatelink.

Copy the value of the Service Name field from the command’s output. In the previous example, the service name is com.amazonaws.vpce.us-east-1.vpce-svc-0410a2e25933fe8ec. You need this value to provision the Amazon VPC endpoint.

Provisioning the Amazon VPC Endpoint

You perform the steps in this section from your Amazon VPC dashboard.

Step 1: Create and Configure a Security Group

Your endpoint requires a security group with appropriate ingress security rules. To create a security group:

  1. Click Create security group in the Security Groups tab of your VPC dashboard.
  2. In the Basic details section, specify a security group name and description, and select your desired VPC.
  3. In the Inbound rules section, click to Add rule.
  4. Enable TCP access to ports 5432–5433 from any valid IP address.
  5. Click Create security group.

The Create security group window on the AWS Console

Step 2: Create the Endpoint

To create an endpoint:

  1. Navigate to the Endpoints tab of your VPC dashboard and click Create Endpoint.
  2. Give a name for your endpoint in the Name tag field.
  3. In the Type section, select the PrivateLink Ready partner services category.
  4. In the Service settings section, paste the Service name value you obtained earlier.
  5. Click Verify service to display the list of available subnets.
    The Create Endpoint window on the AWS Console
  6. In the Network Settings section, select the VPC you specified when creating the Security Group.
  7. In the Subnets section, select the subnets to enable and the Subnet ID, and select the IP address type to use.
  8. In the Security group section, select the security group you created in the previous step, and click Create endpoint.
    The Network Settings, Subnets, and Security Groups section of the Create Endpoint window on the AWS Console

You see the endpoint you created with an initial status of pending acceptance, which transitions to available after 5-10 minutes.

You can also confirm it’s available from the Heroku Dashboard. Navigate to the Settings tab in your database and click Show Configuration under the Heroku Postgres via PrivateLink section. You can see the connection status under Connections.

Connecting the Heroku and Amazon VPC Endpoints

After the Amazon VPC endpoint becomes available, you can obtain the URLs from the config vars that Heroku creates to allow your VPC to communicate with your Heroku Postgres database (DATABASE_ENDPOINT_ID_URL) and PgBouncer (DATABASE_ENDPOINT_ID_PGBOUNCER_URL).

First, obtain your PrivateLink endpoint’s endpoint ID and extract the 17-character string that appears at the end of it. Convert that string to upper case and use it in the following command. For example, if the endpoint ID is vpce-01c87ae3c05563935, the endpoint ID is 01C87AE3C05563935.

Run the command, substituting the the string in ENDPOINT_ID_HERE:

$ heroku config --app your_app_name | grep ENDPOINT_ID_HERE

This command displays the AWS VPC endpoint connection URL and the corresponding connection string for your Heroku Postgres database. The connection string has the following format:

postgres://user:password@vpc-endpoint-dns-name:5432/database

You can now use this connection string to connect the applications in your AWS VPC to your private Heroku Postgres database and PgBouncer instance. Here’s an example command with the output:

$ heroku config --app privatelink-vpc-endpoint-demo | grep 01C87AE3C05563935

DATABASE_ENDPOINT_01C87AE3C05563935_URL:           postgres://abcdefghijklmn:abcdefghijklmnopqrstuvwxyz123456789101112131415161718192021222324@ec2-3-83-63-168.compute-1.amazonaws.com:5432/dd0k757ojc5qt
DATABASE_ENDPOINT_01C87AE3C05563935_PGBOUNCER_URL: postgres://abcdefghijklmn:abcdefghijklmnopqrstuvwxyz123456789101112131415161718192021222324@ec2-3-83-63-168.compute-1.amazonaws.com:5433/dd0k757ojc5qt

You can also get the endpoint ID from the Heroku Dashboard:

  1. Navigate to your database in the Heroku Dashboard and click the Settings tab.
  2. Click Show Configuration next to Heroku Postgres via PrivateLink section.
  3. Click Reveal Credentials to show the endpoint ID.

For any issues or concerns with using this feature, open a support ticket.

Connecting to Heroku Postgres from EC2 via VPC Endpoints

After you configure your VPC endpoints, you can create an EC2 instance in your AWS VPC to connect to Heroku Postgres.

In your EC2 dashboard, click Launch an instance. Give your instance a name and select the AMI type.

Launch an instance AMI window of the EC2 Dashboard

Then, select the instance type and key pair name. The example creates an Ubuntu t2.micro instance.

Launch an instance Instance Type window of the EC2 Dashboard

When configuring the network settings, select the VPC network with the security group you created earlier and pick an appropriate subnet. Review the summary of your instance and click Launch instance.

Launch an instance Network Settings window of the EC2 Dashboard

After the instance’s status transitions to running and all status checks have passed, connect to the instance using the SSH key pair you specified during instance creation.

After it’s connected, install Postgres on your EC2 instance with the commands:

$ sudo apt-get -qq update && sudo apt-get install -y curl ca-certificates
$ curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ sudo apt-get -qq update
$ sudo apt-get install -y postgresql-client-16

You can then connect to your Heroku Postgres database from the EC2 instance using the command:

$ psql postgres://user:password@vpc-endpoint-dns-name:5432/database

The following screenshot shows the connection string being used to connect to a Heroku Postgres database from within a sample EC2 instance:

Connecting to private Postgres database from VPC EC2 Instance

Limitations

Comparison to Private Space Trusted IP Ranges

Private Spaces support trusted IP ranges for data services as a beta feature. This feature is an option if you must connect to a Heroku Postgres database from outside the Private Space boundary. You must contact Heroku to enable this feature, and granular access control isn’t available for it.

By connecting via AWS PrivateLink, your database is treated as part of your own VPC, and you can restrict access to a set of users and roles. Consequently, this method is recommended whenever it’s available for your use case.