使用psql以ssl模式连接到postgresql

问题描述 投票:58回答:5

我正在尝试为postgreSQL服务器配置ssl证书。我已经在数据目录中创建了一个证书文件(server.crt)和密钥(server.key),并将参数SSL更新为“ on”以启用安全连接。

我只希望仅在客户端使用服务器证书对服务器进行身份验证,而不要求在服务器端使用客户端的真实性。我使用psql作为客户端来连接和执行命令。

我正在使用PostgreSQL 8.4和linux。我尝试使用以下命令连接到启用了ssl的服务器]

       psql "postgresql://localhost:2345/postgres?sslmode=require"

但是我得到了

       psql: invalid connection option "postgresql://localhost:2345/postgres?sslmode"

这里做错了什么?我尝试在启用ssl模式的情况下连接服务器的方法是否正确?只验证服务器而不验证客户端是否可以?

请帮帮我。

postgresql ssl ssl-certificate postgresql-8.4
5个回答
91
投票
9.2以下的[[psql]不接受选项的这种类似URL的语法。

可以通过命令行中的sslmode=value选项或PGSSLMODE环境变量来驱动SSL的使用,但是默认值为prefer,将首先自动尝试不指定任何内容的SSL连接。

带有conninfo字符串的示例(

已为psql 8.4更新]

psql "sslmode=require host=localhost dbname=test"
阅读manual page了解更多选项。

13
投票
psql --set=sslmode=require -h localhost -p 2345 -U thirunas \ -d postgres -f test_schema.ddl

2
投票
psql "sslmode=require host=localhost port=2345 dbname=postgres" --username=some_user

根据postgres psql documentation,仅连接参数应放在conninfo字符串中(这就是我们的示例中--username不在该字符串内的原因)


1
投票
psql -h <host> -p <port> -U <user> -d <db>
并更新/var/lib/pgsql/10/data/pg_hba.conf以将auth方法更改为cert。检查以下链接以获取更多信息:

https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html


0
投票
发现以下用于为自签名postgres实例提供所有文件的选项

psql "host={hostname} sslmode=prefer sslrootcert={ca-cert.pem} sslcert={client-cert.pem} sslkey={client-key.pem} port={port} user={user} dbname={db}"

© www.soinside.com 2019 - 2024. All rights reserved.