postgresql connection string
在线生成适用于应用、驱动和 psql 的 PostgreSQL 连接串
一次填写连接信息,同时生成 PostgreSQL URI、JDBC、libpq、SQLAlchemy 和 psql 命令行格式。
适合在多个框架、驱动和命令行工具之间复用同一套数据库账号信息。
连接信息
输出选项
生成结果
按你当前使用的技术栈,直接复制对应格式即可。
PostgreSQL URI
适合配置文件和 DATABASE_URL 这类环境变量场景。
postgresql://postgres:secret@127.0.0.1:5432/app_db?sslmode=preferJDBC
适合 Java、Spring 等需要 JDBC URL 的场景。
jdbc:postgresql://127.0.0.1:5432/app_db?user=postgres&password=secret&sslmode=preferlibpq 键值格式
适合 PostgreSQL 原生客户端和基于 libpq 的工具。
host=127.0.0.1 port=5432 dbname=app_db user=postgres password=secret sslmode=preferSQLAlchemy
适合 Python 项目里使用 SQLAlchemy + psycopg 的场景。
postgresql+psycopg://postgres:secret@127.0.0.1:5432/app_db?sslmode=preferpsql 命令行
适合先在终端中用 psql 快速验证连接。
psql "host=127.0.0.1 port=5432 dbname=app_db user=postgres sslmode=prefer"这个 PostgreSQL 连接串生成器能帮你做什么
在多个工具之间复用同一套账号信息
把同一组 host、port、用户名和密码,转换成应用、驱动或命令行真正需要的格式。
适配云上 PostgreSQL 的安全连接
可以直接带上 SSL mode,适合托管 PostgreSQL 服务这类默认要求安全连接的场景。
先用 psql 验证,再接入项目
可以先复制 psql 命令测试连通性和账号是否正确,再放进应用配置里。
常见示例
应用配置
postgresql://app_user:secret@db.example.com:5432/app_db?sslmode=requireJDBC
jdbc:postgresql://db.example.com:5432/app_db?user=app_user&password=secret&sslmode=requirepsql
psql "host=db.example.com port=5432 dbname=app_db user=app_user sslmode=require"SQLAlchemy
postgresql+psycopg://app_user:secret@db.example.com:5432/app_db?sslmode=requirelibpq 键值格式
host=db.example.com port=5432 dbname=app_db user=app_user password=secret sslmode=require connect_timeout=10同一个连接的几种叫法
文档和配置文件里对同一组连接信息的叫法并不一致。下面这三种说法指向的都是 host、port、库名、账号和 SSL 参数,只是写法和使用场景不同。
psql connection string:命令行怎么写
psql 可以接收 conninfo 字符串、URI,或者拆开的参数。上面的生成器会给出前两种,临时在终端里测一次连接时用参数形式最方便。
psql "host=db.internal.example.net port=5432 dbname=orders user=reporting sslmode=require"
psql postgresql://reporting:secret@db.internal.example.net:5432/orders?sslmode=require -c "select version();"
psql -h db.internal.example.net -p 5432 -U reporting -d orders -c "select 1;"命令行里不要写密码,避免留在 shell history 中;psql 会自己提示输入。
postgres DSN:libpq 键值形式
说 postgres DSN 时,通常指 libpq 的 conninfo 字符串:一组用空格分隔的 key=value。它同样可以拆成 PGHOST、PGPORT 这类环境变量。
host=db.internal.example.net port=5432 dbname=orders user=reporting password=secret sslmode=require connect_timeout=10
PGHOST=db.internal.example.net PGPORT=5432 PGDATABASE=orders PGUSER=reporting psql -c "select 1;"值里带空格要加引号;改写成 URI 时,@ : / # 等字符要做 percent-encode。
postgresql DSN:应用配置里的几种写法
Java、Python 和 .NET 驱动会写全产品名,并把同一组参数包进各自的 DSN 语法里。下面是最常粘进配置文件的三种。
jdbc:postgresql://db.internal.example.net:5432/orders?user=reporting&password=secret&sslmode=require
postgresql+psycopg://reporting:secret@db.internal.example.net:5432/orders?sslmode=require
Host=db.internal.example.net;Port=5432;Database=orders;Username=reporting;Password=secret;SSL Mode=Require上面生成器里的 JDBC、SQLAlchemy 和 URI 输出,用的就是同样这组值。
PostgreSQL 连接常见报错
连不上数据库,多数落在四类原因里:网络不通、账号不对、库名写错、SSL 配置不匹配。按第一列找到你看到的那条提示。
| 报错信息 | 原因 | 怎么改 |
|---|---|---|
| psql: command not found | 本机没装 psql,或者它的 bin 目录不在 PATH 里。 | 只装客户端用 brew install libpq,再把 $(brew --prefix libpq)/bin 加进 PATH;用 Postgres.app 就加它自带的 bin 目录。 |
| could not connect to server: Connection refused | 该地址和端口没有服务在监听,或者被防火墙、云安全组拦住了。 | 在 Mac 上跑 nc -vz db.example.com 5432 测连通,再检查 listen_addresses、postgresql.conf 里的端口和云安全组。 |
| password authentication failed for user "app_user" | 账号或密码不对、pg_hba.conf 不允许这个来源,或者密码里的特殊字符把 URI 截断了。 | 先用 psql -h db.example.com -U app_user -d app_db 单独验证,再把 URI 里的 @ : / # 等字符做 percent-encode。 |
| FATAL: database "app_db" does not exist | 库名拼错,或者连接串指到了默认库。 | 用 psql -l 列出实际库名,改掉 URI 最后一个斜杠后面的名字。 |
| server does not support SSL, but SSL was required | sslmode=require 和服务端的 TLS 配置不一致。 | 可信的内网用 sslmode=disable 或 prefer;托管 PostgreSQL 通常要 require,并确认服务端证书有效。 |
| relation "orders" does not exist | 表存在,但不在当前 search_path 里。 | 在 URI 里加 options=-csearch_path%3Dmyschema,或者把表名写成 myschema.orders。 |
相关工具
MySQL 连接串生成
同样填一次连接信息,生成 MySQL 的 JDBC、命令行和 SQLAlchemy 格式。
Redis 连接串生成
应用还要接缓存或队列时,生成 redis:// 与 rediss:// 地址。
SSH 端口转发命令生成
数据库只监听内网时,用一条 SSH 隧道从本机连过去。