Centos7安装postgresql

本文Centos版本为7.4,安装的Postgresql版本为9.4。

1.查找需要安装的版本:

可以下载9.3以下的版本:https://yum.postgresql.org/repopackages.php

查看9.4以上的版本:https://download.postgresql.org/pub/repos/yum/

2.使用root用户登录,安装yum源:

yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm

下载路径为:

[root@localhost yum-root-v9tlUe]# pwd
/var/tmp/yum-root-v9tlUe
[root@localhost yum
-root-v9tlUe]# ls pgdg-centos94-9.4-3.noarch.rpm

 3.安装Postgresql

yum install -y postgresql94-server postgresql94-contrib

初始化数据库:

查看数据库有没有初始化

ll -lhtr /var/lib/pgsql/9.4/data/

如果初始化过,把之前的数据库删除掉,在初始化。

rm -rf /var/lib/pgsql/9.4/data/*
/var/lib/pgsql
/usr/pgsql-9.4

初始化:

/usr/pgsql-9.4/bin/postgresql94-setup initdb

4.修改Postgresql用户密码(安装完之后,会自动生成一个postgres用户)

切换postgres用户:

su - postgres

登录数据库:

psql -U postgres

可能会出现以下错误:(重启psql再进入,systemctl restart postgresql-9.4.service)

 

更新密码:

ALTER USER postgres with encrypted password 'abc123';

5.配置远程访问:

vi /var/lib/pgsql/9.4/data/postgresql.conf

找到listen_addresses = 'localhost' ,将 localhost 改为 *

vi /var/lib/pgsql/9.4/data/pg_hba.conf

如果允许本地项目访问,将local、IPv4、IPv6的peer改为trust

并在IPv4增加一行:

host    all    all    0.0.0.0/0    md5

 

重启服务:

systemctl restart postgresql-9.4.service

防火墙设置:

firewall-cmd --add-service=postgresql --permanent
firewall-cmd --reload

可以用其他工具连接。

6.相关命令

systemctl restart postgresql-9.4.service    #重启服务
systemctl enable postgresql-9.4.service    #设置开机自启
systemctl start postgresql-9.4.service    #开启服务
systemctl status postgresql-9.4.service    #查看服务状态

数据库相关操作:

q    #退出postgresql
l    #查看所有数据库
dt  #查看数据库表
d test  #查看表结构
c dbname  #切换数据库 i
/pathA/xxx.sql #执行某个sql文件
原文地址:https://www.cnblogs.com/Yatces/p/11082061.html