Postgresql 安装与连接 笔记

需求

在远程主机(ubuntu 18.04)上安装postgres(当前稳定版为12),然后用Datagrip在本地端连接。

安装PG

ubuntu上执行apt 安装pg,命令如下:

# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get install postgresql

安装完成后,用psql试图以postgres的身份登录,发现登录不了。错误信息大意为:

 Peer authentication failed for user "postgres"

原因为当前os安装用户名并非postgres。解决办法:找到配置文件/etc/postgresql/12/main/pg_hba.conf

将其中的 

local   all             postgres                                peer

修改为

local   all             postgres                                md5

保存配置文件的修改。然后重启pg服务:

/etc/init.d/postgresql restart

或者  sudo service postgresql restart

然后便可在远程主机用psql登录pg。

用Datagrip连接

本地端(Windows)安装Datagrip略过不表。安装后打开Datagrip,填好相关连接信息后,Test Connection 发现结果失败,连接不了。

解决思路和办法:

1,远程主机为云主机,默认的防火墙设置是没有放行5432端口的。需要登录控制台,修改防火墙设置(并非os的防火墙,而是云主机服务商的防火墙。)

如果需求为用程序直接连接,应该修改防火墙设置;且修改远程主机中/etc/postgresql/12/main/pg_hba.conf,以便允许客户端连接。考虑到当下主要是用DG连接,暂时并没有程序直接连接的需求。故采用下一种方法。

2,打开Data Source的设置,切换到SSH/SSL画面(如下图)。选择Key Pair 的方式,配置相关信息后,Test Connection成功。

注意:

1,General选项卡中,此时Host应填写localhost(如下图)。

2,如果还未和远程主机设置免密码登录,可使用ssh-keygen和ssh-copy-id命令。

 

参考链接:

https://www.postgresql.org/download/linux/ubuntu/

http://www.voidcn.com/article/p-gniwgbkg-bty.html

https://www.cnblogs.com/102442/p/9923417.html

https://blog.csdn.net/YTREE_BJ/article/details/103760271

https://jingyan.baidu.com/article/b87fe19e273e44521835681a.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值