콘텐츠로 이동
Docs Try Aspire

Connect to PostgreSQL

이 콘텐츠는 아직 번역되지 않았습니다.

PostgreSQL logo

When you reference a PostgreSQL resource from the AppHost with WithReference (C#) or withReference (TypeScript), Aspire automatically injects connection information into the consuming application as environment variables. This page shows how to read those variables and connect to PostgreSQL from any language.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called postgresdb becomes POSTGRESDB_URI.

The PostgreSQL server resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the PostgreSQL server
PortThe port number the PostgreSQL server is listening on
UsernameThe username for authentication
PasswordThe password for authentication
UriThe connection URI in postgresql:// format, with the format postgresql://{Username}:{Password}@{Host}:{Port}
JdbcConnectionStringJDBC-format connection string, with the format jdbc:postgresql://{Host}:{Port}. User and password credentials are provided as separate Username and Password properties.

Example connection strings:

Uri: postgresql://postgres:p%40ssw0rd1@localhost:5432
JdbcConnectionString: jdbc:postgresql://localhost:5432

The PostgreSQL database resource inherits all properties from its parent server resource and adds:

Property NameDescription
UriThe connection URI with the database name, with the format postgresql://{Username}:{Password}@{Host}:{Port}/{DatabaseName}
JdbcConnectionStringJDBC connection string with database name, with the format jdbc:postgresql://{Host}:{Port}/{DatabaseName}. User and password credentials are provided as separate Username and Password properties.
DatabaseNameThe name of the database

Example connection strings:

Uri: postgresql://postgres:p%40ssw0rd1@localhost:5432/catalog
JdbcConnectionString: jdbc:postgresql://localhost:5432/catalog

The following examples show how to connect to PostgreSQL from different languages. Each example assumes you have a PostgreSQL database resource named postgresdb referenced from your AppHost.

Install the PostgreSQL client library:

Terminal
npm install pg

Read the injected environment variables and connect:

JavaScript — index.js
import pg from 'pg';
// Read Aspire-injected connection properties
const client = new pg.Client({
user: process.env.POSTGRESDB_USERNAME,
host: process.env.POSTGRESDB_HOST,
database: process.env.POSTGRESDB_DATABASENAME,
password: process.env.POSTGRESDB_PASSWORD,
port: process.env.POSTGRESDB_PORT,
});
await client.connect();

Or use the connection URI directly:

JavaScript — Connect with URI
const client = new pg.Client({
connectionString: process.env.POSTGRESDB_URI,
});
await client.connect();

Passing custom environment variables from the AppHost

Section titled “Passing custom environment variables from the AppHost”

If your application expects specific environment variable names, you can pass individual connection properties from the AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres");
var database = postgres.AddDatabase("myDatabase");
var app = builder.AddExecutable("my-app", "node", "app.js", ".")
.WithReference(database)
.WithEnvironment(context =>
{
context.EnvironmentVariables["POSTGRES_HOST"] = postgres.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
context.EnvironmentVariables["POSTGRES_PORT"] = postgres.Resource.PrimaryEndpoint.Property(EndpointProperty.Port);
context.EnvironmentVariables["POSTGRES_USER"] = postgres.Resource.UserNameParameter;
context.EnvironmentVariables["POSTGRES_PASSWORD"] = postgres.Resource.PasswordParameter;
context.EnvironmentVariables["POSTGRES_DATABASE"] = database.Resource.DatabaseName;
});
builder.Build().Run();