PostgreSQL 连接串URI配置(libpq兼容配置)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

标签

PostgreSQL , libpq , 连接串 , URI , options , jdbc


背景

连接数据库是最基本的操作之一,PostgreSQL libpq支持URI的连接模式,格式如下:

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]  
AI 代码解读

例子

postgresql://  
postgresql://localhost  
postgresql://localhost:5433  
postgresql://localhost/mydb  
postgresql://user@localhost  
postgresql://user:secret@localhost  
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp  
postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp  
postgresql:///mydb?host=localhost&port=5433  
postgresql://[2001:db8::1234]/database  
postgresql:///dbname?host=/var/lib/postgresql  
postgresql://%2Fvar%2Flib%2Fpostgresql/dbname  
AI 代码解读

从10开始,支持连接多个主机

postgresql://host1:port1,host2:port2,host3:port3/  
AI 代码解读

出了使用URI的写法,还支持这种格式

host=localhost port=5432 dbname=mydb connect_timeout=10  
AI 代码解读

连接时,支持设定一些连接参数,例如application_name,target_session_attrs等等。还有一些数据库client参数也可以通过options这个参数传入(例如timezone),在建立连接后自动设置。

URI中支持的parameter详见:

https://www.postgresql.org/docs/10/static/libpq-connect.html

接下来使用psql来验证这个方法

连接时如何设置客户端参数

使用psql客户端进行验证

man psql

-d dbname  
--dbname=dbname  
    Specifies the name of the database to connect to.   
    This is equivalent to specifying dbname as the first non-option argument on the command line.  
  
    If this parameter contains an = sign or starts with a valid URI prefix   
    (postgresql:// or postgres://), it is treated as a conninfo string.   
    See Section 33.1.1 for more information.  
AI 代码解读

对于其他URI中非直接支持的客户端参数,需要通过options这个参数来进行设置

options  
  
Specifies command-line options to send to the server at connection start.   
  
For example, setting this to -c geqo=off sets the session's value of the geqo parameter to off.   
  
Spaces within this string are considered to separate command-line arguments, unless escaped with a backslash (\);   
  
write \\ to represent a literal backslash.   
  
For a detailed discussion of the available options, consult Chapter 19.  
AI 代码解读

与psql类似,postgres命令也支持类似方法设置启动参数

man postgres

-o extra-options  
    The command-line-style arguments specified in extra-options are   
    passed to all server processes started by this postgres process.  
  
    Spaces within extra-options are considered to separate arguments,   
    unless escaped with a backslash (\); write \\ to represent a literal backslash.   
    Multiple arguments can also be specified via multiple uses of -o.  
  
    The use of this option is obsolete;   
    all command-line options for server processes can be specified directly on the postgres command line.  
AI 代码解读

使用psql验证非标准参数的连接参数的设置

1、比如我们需要设置客户端时区,连接时设置。

psql -d "host=127.0.0.1 port=1921 options='-c timezone=+10'"  
  
psql (10beta4)  
Type "help" for help.  
  
postgres=# show timezone;  
 TimeZone   
----------  
 <+10>-10  
(1 row)  
  
postgres=# select now();  
              now                
-------------------------------  
 2017-09-12 23:57:58.174722+10  
(1 row)  
AI 代码解读

2、又比如,我们设置标准参数(即URI直接支持的参数)

psql postgres://postgres@127.0.0.1:1921/postgres?application_name=abc  
psql (10beta4)  
Type "help" for help.  
  
postgres=# show application_name ;  
 application_name   
------------------  
 abc  
(1 row)  
  
postgres=# \q  
AI 代码解读

3、直接设置非标准参数,会导致合法性建议报错

psql postgres://postgres@127.0.0.1:1921/postgres?timezone=+10  
psql: invalid URI query parameter: "timezone"  
AI 代码解读

所以options参数,就是提供设置这种非标准参数的。

4、注意,psql在解析URI的options参数内容时,等号需要用%3D代替,这种写法,导致无法设置成功非标准参数

psql postgres://postgres@127.0.0.1:1921/postgres?options='-c TimeZone=+10'  
psql: extra key/value separator "=" in URI query parameter: "options"  
AI 代码解读

正确写法

psql postgres://postgres@127.0.0.1:1921/postgres?options='-c TimeZone%3D+10 -c extra_float_digits%3D2'

postgres=# show timezone;
 TimeZone 
----------
 <+10>-10
(1 row)

postgres=# show extra_float_digits ;
 extra_float_digits 
--------------------
 2
(1 row)
AI 代码解读

通过SET命令设置会话、事务级参数

如果以上方法无法满足非标准参数的设置,那么你还有两种方法可以实现非标准参数的设置,以timezone为例。

连接成功后,或者首次连接后,自动执行如下:  
  
set timezone=+10;  
AI 代码解读

通过配置database, role默认参数,设置会话参数

第二种方法,设置database, role的默认参数。例子

alter role all|username set timezone=+10;  
  
alter database dbname set timezone=+10;  
AI 代码解读

参考

https://www.postgresql.org/docs/10/static/libpq-connect.html

https://jdbc.postgresql.org/documentation/head/connect.html

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
打赏
0
0
0
0
20695
分享
相关文章
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
在Ubuntu 22.04上配置和安装MySQL
以上就是在Ubuntu 22.04上配置和安装MySQL的步骤。这个过程可能看起来有点复杂,但只要按照步骤一步步来,你会发现其实并不难。记住,任何时候都不要急于求成,耐心是解决问题的关键。
58 30
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
205 26
MySQL 8.4 配置SSL组复制(八个步骤)
MySQL 8.4 配置SSL组复制(八个步骤)
41 0
seatunnel配置mysql2hive
本文介绍了SeaTunnel的安装与使用教程,涵盖从安装、配置到数据同步的全过程。主要内容包括: 1. **SeaTunnel安装**:详细描述了下载、解压及配置连接器等步骤。 2. **模拟数据到Hive (fake2hive)**:通过编辑测试脚本,将模拟数据写入Hive表。 3. **MySQL到控制台 (mysql2console)**:创建配置文件并执行命令,将MySQL数据输出到控制台。 4. **MySQL到Hive (mysql2hive)**:创建Hive表,配置并启动同步任务,支持单表和多表同步。
179 15
mysql主从复制概述和配置
【10月更文挑战第22天】MySQL 主从复制是一种将主服务器的数据复制到一个或多个从服务器的技术,实现读写分离,提高系统性能和可用性。主服务器记录变更日志,从服务器通过 I/O 和 SQL 线程读取并应用这些变更。适用于读写分离、数据备份和恢复、数据分析等场景。配置步骤包括修改配置文件、创建复制用户、配置从服务器连接主服务器并启动复制进程。
293 1
PostgreSQL 入门指南:安装、配置与基本命令
本文从零开始,详细介绍如何在 Windows、Linux 和 macOS 上安装和配置 PostgreSQL,涵盖30+个实操代码示例。内容包括安装步骤、配置远程访问和用户权限、基础数据库操作命令(如创建表、插入和查询数据),以及常见问题的解决方案。通过学习,你将掌握 PostgreSQL 的基本使用方法,并为后续深入学习打下坚实基础。
1356 1
2024Mysql And Redis基础与进阶操作系列(1)作者——LJS[含MySQL的下载、安装、配置详解步骤及报错对应解决方法]
Mysql And Redis基础与进阶操作系列(1)之[MySQL的下载、安装、配置详解步骤及报错对应解决方法]
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
526 2

热门文章

最新文章

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版