首页 > 数据库 >非交互式连接PostgreSQL

非交互式连接PostgreSQL

时间:2023-03-01 14:23:31浏览次数:42  
标签:psql postgresql help 0.0 pgpass 交互式 PostgreSQL 连接 postgres

方法一:.pgpass

查看postgres家目录

grep postgres /etc/passwd
postgres:x:114:119:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash

创建.pgpass

touch /var/lib/postgresql/.pgpass
chown -R postgres.postgres /var/lib/postgresql/.pgpass
chmod 0600 /var/lib/postgresql/.pgpass

文件格式

hostname::::port_ database_ username_password

示例

echo "192.168.1.23:5432:postgres:postgres:postgres" >> /var/lib/postgresql/.pgpass

连接测试

su - postgres -c "psql -U postgres -h 192.168.1.23 -p 5432 -w "
psql (15.2 (Ubuntu 15.2-1.pgdg20.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

postgres=# 

方法二:设置环境变量 PGPASSWORD

su - postgres -c " export PGPASSWORD="postgres" && psql -U postgres -h 192.168.1.23 -p 5432 "
psql (15.2 (Ubuntu 15.2-1.pgdg20.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

postgres=# 

方法三:auth-method trust

pg_hba.conf

echo "host    postgres      postgres         0.0.0.0/0           trust" >> /etc/postgresql/15/main/pg_hba.conf

重启服务

systemctl restart postgresql.service

测试远程连接

su - postgres -c " psql -U postgres  -h 192.168.1.23 -p 5432 "
psql (15.2 (Ubuntu 15.2-1.pgdg20.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

postgres=# 

注意事项

host    postgres      postgres         0.0.0.0/0           trust
host    all             all             0.0.0.0/0            scram-sha-256
# trust 认证规则需要在scram-sha-256规则之前,否则trust不生效

标签:psql,postgresql,help,0.0,pgpass,交互式,PostgreSQL,连接,postgres
From: https://www.cnblogs.com/wangguishe/p/17168032.html

相关文章

  • ubuntu20.04二进制部署PostgreSQL 15.2
    创建文件存储库配置echo"debhttp://apt.postgresql.org/pub/repos/apt$(lsb_release-cs)-pgdgmain">/etc/apt/sources.list.d/pgdg.list导入存储库签名密钥wget......
  • HTTPS连接过程
    简介HTTPS是在HTTP的基础上和ssl/tls证书结合起来的一种协议,保证了传输过程中的安全性,减少了被恶意劫持的可能.很好的解决了解决了http的三个缺点(被监听、被篡改、被伪......
  • 基于Matlab模拟交互式玻璃化转变分析仪 (DSC)
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • Java连接数据库技术-JDBC
    课程简介和目标 Java数据库连接,(JavaDatabaseConnectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据......
  • Java连接数据库技术-JDBC
    课程简介和目标 Java数据库连接,(JavaDatabaseConnectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据......
  • 连接查询
    一、(知道)数据查询操作1、连接查询-常用方式内连接:连接两个表时,取的是两个表中都存在的数据左连接:连接两个表时,取的是左表中特有的数据,对于右表中不存在的......
  • Postgresql获取指定字符后的末尾字符串
    需求:妍妍-肉肉,需要获取-后面的肉肉selectsubstr('妍妍-肉肉',length(substr(reverse('妍妍-肉肉'),position('-'inreverse('妍妍-肉肉'))))+1)输出:肉肉可以看下......
  • Linux-软连接/时期,时区
             ......
  • 轮询、长轮询、长连接、websocket
    前言Web端即时通讯技术:即时通讯技术简单的说就是实现这样一种功能:服务器端可以即时地将数据的更新或变化反应到客户端,例如消息即时推送等功能都是通过这种技术实现的。但......
  • PostgreSQL备份脚本
    1.直接上代码#!/bin/bash#数据库名称db_name="odoo14e"#存放备份文件的url地址base_url="/home/odoo/pgsql_backup/backup_files/"#判断是否存在存放备份文......