搜索插件、代码仓与 APP 分类

插件详情v3.6.0

postgres

PostgreSQL 数据库驱动程序。支持二进制协议、连接池和语句重用。

AndroidiOSmacOSWindowsLinux
近 30 天下载
60.4万
喜欢数
416
Pub 分数
160

安装插件

flutter pub add postgres

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

postgres 包是 Dart 和 Flutter 的 PostgreSQL 数据库驱动程序,支持二进制协议、连接池和语句重用,可实现高效的数据库交互。它使开发者能够构建需要跨多个平台的可靠数据库连接的可扩展应用程序。该驱动程序以性能和可靠性为设计目标,适用于小型项目和大型系统。它可无缝集成到 Flutter 应用中,并支持所有主要操作系统,包括 Android、iOS、macOS、Windows 和 Linux。

适合场景

  • 将 Flutter 应用连接到 PostgreSQL 数据库
  • 使用实时数据构建后端服务
  • 实现安全高效的数据库查询
  • 管理高容量数据库操作
  • 支持跨平台数据库访问

主要特点

  • 二进制协议支持
  • 连接池
  • 语句重用
  • 跨平台兼容性
  • 强大的错误处理

适合对象

  • 需要数据库访问的Flutter移动应用程序
  • 使用PostgreSQL的后端服务
  • 需要高性能数据库操作的应用程序
  • 针对多个平台的开发者
  • 需要可靠数据库连接的项目

使用建议

  • 需要设置 PostgreSQL 服务器
  • 可能需要为生产环境进行额外配置
  • 限制为 PostgreSQL 数据库类型
  • 性能取决于网络和服务器配置
  • 需要设置身份验证以实现安全访问

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

PostgreSQL client

CI

A library for connecting to and querying PostgreSQL databases (see Postgres Protocol). This driver uses the more efficient and secure extended query format of the PostgreSQL protocol.

Usage

Create a Connection:

  final conn = await Connection.open(Endpoint(
    host: 'localhost',
    database: 'postgres',
    username: 'user',
    password: 'pass',
  ));

Execute queries with execute:

  final result = await conn.execute("SELECT 'foo'");
  print(result[0][0]); // first row and first field

Named parameters, returning rows as map of column names:

  final result = await conn.execute(
    Sql.named('SELECT * FROM a_table WHERE id=@id'),
    parameters: {'id': 'xyz'},
  );
  print(result.first.toColumnMap());

Execute queries in a transaction:

  await conn.runTx((s) async {
    final rs = await s.execute('SELECT count(*) FROM foo');
    await s.execute(
      r'UPDATE a_table SET totals=$1 WHERE id=$2',
      parameters: [rs[0][0], 'xyz'],
    );
  });

See the API documentation: https://pub.dev/documentation/postgres/latest/

Connection string URLs

The package supports connection strings for both single connections and connection pools:

await Connection.openFromUrl('postgresql://localhost/mydb');
await Connection.openFromUrl(
  'postgresql://user:pass@db.example.com:5432/production?sslmode=verify-full'
);
await Connection.openFromUrl(
  'postgresql://localhost/mydb?connect_timeout=10&query_timeout=60'
);
Pool.withUrl(
  'postgresql://localhost/mydb?max_connection_count=10&max_connection_age=3600'
);
URL Format

postgresql://[userspec@][hostspec][:port][/dbname][?paramspec]

  • Scheme: postgresql:// or postgres://
  • User: username or username:password (can also be set via user/username and password query parameters)
  • Host: hostname or IP address (defaults to localhost). Supports multiple hosts via comma-separated list (host1:5433,host2:5434) or multiple host query parameters (?host=host1:5433&host=host2:5434)
  • Port: port number (defaults to 5432, can be overridden via port query parameter)
  • Database: database name (defaults to postgres, can be overridden via database query parameter)
  • Parameters: query parameters (see below)
Standard Parameters

These parameters are supported by Connection.openFromUrl():

Parameter Type Description Example Values
application_name String Sets the application name application_name=myapp
client_encoding String Character encoding UTF8, LATIN1
connect_timeout Integer Connection timeout in seconds connect_timeout=30
database String Database name (overrides URL path) database=mydb
host String Alternative host specification (supports Unix sockets) host=/var/run/postgresql, host=host1:5433
password String Password (overrides URL userspec) password=secret
port Integer Port number (overrides URL port) port=5433
user / username String Username (overrides URL userspec) user=myuser
sslmode String SSL mode disable, require, verify-ca, verify-full
sslcert String Path to client certificate sslcert=/path/to/cert.pem
sslkey String Path to client private key sslkey=/path/to/key.pem
sslrootcert String Path to root certificate sslrootcert=/path/to/ca.pem
replication String Replication mode database (logical), true/physical, false/no_select (none)
query_timeout Integer Query timeout in seconds query_timeout=300
Pool-Specific Parameters

These additional parameters are supported by Pool.withUrl():

Parameter Type Description Example Values
max_connection_count Integer Maximum number of concurrent connections max_connection_count=20
max_connection_age Integer Maximum connection lifetime in seconds max_connection_age=3600
max_session_use Integer Maximum session duration in seconds max_session_use=600
max_query_count Integer Maximum queries per connection max_query_count=1000

Connection pooling

The library supports connection pooling (and masking the connection pool as regular session executor).

Custom type codecs

The library supports registering custom type codecs (and generic object encoders) through theConnectionSettings.typeRegistry.

Streaming replication protocol

The library supports connecting to PostgreSQL using the Streaming Replication Protocol. See Connection documentation for more info. An example can also be found at the following repository: postgresql-dart-replication-example

Other notes

This library originally started as StableKernel's postgres library, but got a full API overhaul and partial rewrite of the internals.

Please file feature requests and bugs at the issue tracker.

所属分类

依赖

依赖清单 9 项

源码仓库

使用此插件的项目

所在榜单