Skip to main content

PostgreSQL

Use PostgreSQL for production‑grade features (JSONB, window functions, robust constraints). Configure via URL or discrete fields.

Install

Add the driver package:

dependencies:
ormed: ^latest
ormed_postgres: ^latest

Configure (ormed.yaml)

driver:
type: postgres
options:
url: postgres://postgres:postgres@localhost:6543/orm_test
sslmode: disable # disable | require | verify-full
schema: public

Use in code (DataSource)

Future<DataSource> createPostgresDataSourceFromUrl() async {
final dataSource = DataSource(
DataSourceOptions(
name: 'default',
driver: PostgresDriverAdapter.fromUrl(
'postgres://postgres:postgres@localhost:5432/app',
),
entities: generatedOrmModelDefinitions,
),
);
await dataSource.init();
return dataSource;
}

Type support

Most PostgreSQL column types map to plain Dart types (int, double, String, bool, DateTime). For richer Postgres-specific types, ormed_postgres ships codecs and value types you can use directly in model fields.

Common Postgres-specific mappings

PostgreSQL typeDart typeNotes
uuidUuidValueExact UUID value (from package:uuid).
numeric / decimalDecimalExact decimal math (from package:decimal).
byteaUint8ListBinary payloads.
intervalIntervalMonths/days/microseconds.
tsvectorTsVectorFull‑text search document.
tsqueryTsQueryFull‑text search query.
int4range / int8rangeIntRangeInteger ranges.
daterangeDateRangeDate ranges.
tsrange / tstzrangeDateTimeRangeTimestamp ranges.
inetPgInetIP host address wrapper.
cidrPgCidrNetwork address wrapper.
macaddr / macaddr8PgMacAddressMAC address wrapper.
bit / varbitPgBitStringBit strings (0/1).
moneyPgMoneyStored as integer cents (avoid locale-dependent formatting).
timeTimeTime of day (no date).
timetzPgTimeTzTime of day + UTC offset.
pg_lsnLSNLog sequence number.
pg_snapshot / txid_snapshotPgSnapshotTransaction snapshot (xmin, xmax, xip).
point / line / lseg / box / path / polygon / circlePoint, Line, LineSegment, Box, Path, Polygon, CircleGeometric types (re-exported by ormed_postgres).
json / jsonbMap<String, Object?> / List<Object?>Encoded/decoded as JSON.
final uuid = UuidValue.fromString('00000000-0000-0000-0000-000000000000');
final amount = Decimal.parse('12.34');
final money = const PgMoney.fromCents(1234);
final bits = PgBitString.parse('10101010');
final timeTz = PgTimeTz(
time: Time(1, 2, 3),
offset: const Duration(hours: 4),
);
final lsn = LSN.fromString('0/10');
final snapshot = const PgSnapshot(xmin: 1, xmax: 10, xip: [2, 3]);
final location = const Point(1.5, 2.5);
final payload = Uint8List.fromList([1, 2, 3, 4]);

Options

OptionTypeDefaultDescription
urlString?Full connection string. Aliases: uri, dsn. When set, it becomes the primary source for host/port/database/user/password and may also read sslmode, timezone, and application_name from the URL query parameters.
hostStringlocalhostDatabase host (ignored when url/uri/dsn is set).
portint5432Database port (ignored when url/uri/dsn is set).
databaseStringconnection name or postgresDatabase name (ignored when url/uri/dsn is set).
usernameStringpostgresDatabase username (ignored when url/uri/dsn is set).
passwordString?Database password.
sslmodeStringdisableTLS mode: disable, require, verify-full.
connectTimeoutint? (ms)driver defaultSocket connect timeout in milliseconds.
statementTimeoutint? (ms)driver defaultServer-side query timeout in milliseconds.
timezoneStringUTCSession timezone.
applicationNameString?Tags sessions for monitoring (application_name in URL query is also supported).
schemaStringpublicDefault schema used when qualifying tables for migrations/queries.

Notes

  • Supports RETURNING, JSON/JSONB operators, window functions, and schema introspection.
  • Migrations and seeds are configured separately in ormed.yaml. See Getting Started → Configuration.
  • For plan/previews/schema internals, see Drivers → Internals.