CentOS 7 安装、配置、使用 PostgreSQL 10 安装及基础配置

官网安装方法:https://www.postgresql.org/download/linux/redhat/
卸载的话使用 yum remove 相应的安装
Install the repository RPM:

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm

install the server packages:

yum install postgresql10-server
此时在/usr/下多了pgsql-10目录

关于安装的几个软件的介绍:
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin3 - pgAdmin III graphical administration utility

关于server和client的介绍参见:
https://www.postgresql.org/docs/10/static/tutorial-start.html

安装后:

设置环境变量:

初始化数据库:

/usr/pgsql-10/bin/postgresql-10-setup initdb

可选设置自动启动:

systemctl enable postgresql-10
systemctl start postgresql-10

启动服务:

service postgresql-10 initdb
chkconfig postgresql-10 on

直接执行createdb后会有很多问题,具体看https://www.postgresql.org/docs/10/static/tutorial-start.html

PostgreSQL 安装完成后,会建立一下‘postgres’用户,用于执行PostgreSQL,数据库中也会建立一个’postgres’用户,默认密码为自动生成,需要在系统中改一下。

修改postgres用户密码:

先切换到root
然后passwd postgress
接着输入两遍新密码

修改postgres数据库管理员密码:

su - postgres 切换用户,执行后提示符会变为 ‘-bash-4.2$’,切换为UNIX风格的bash
psql -U postgres 登录数据库,执行后提示符变为 ‘postgres=#’
ALTER USER postgres WITH PASSWORD ‘abc123’ 设置postgres用户密码
\q 退出数据库

切换到root配置一下远程连接。
开启远程访问

vi /var/lib/pgsql/10/data/postgresql.conf
修改#listen_addresses = ‘localhost’ 为 listen_addresses=’*’
当然,此处‘*’也可以改为任何你想开放的服务器IP

信任远程连接

vi /var/lib/pgsql/10/data/pg_hba.conf
修改如下内容,信任指定服务器连接
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.2/32(需要连接的服务器IP) trust

远程连接配置完成,由于系统原因,还需要在防火墙中打开相应的端口。

打开防火墙
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我们只需以服务方式将PostgreSQL服务开放即可。

firewall-cmd –add-service=postgresql –permanent 开放postgresql服务
firewall-cmd –reload 重载防火墙

最后一步,不能忘记的,是重启数据库服务,使配置生效。

重启PostgreSQL数据服务

systemctl restart postgresql-10.service

错误:

Job for postgresql-10.service failed because the control process exited with error code. See "systemctl status postgresql-10.service" and "journalctl -xe" for details
  • 1

原因:
数据库安装完后没有进行初始化工作
解决:

>/usr/pgsql-10/bin/postgresql-10-setup initdb
  • 1

至此,PostgreSQL 10 在CentOS 7上完成基本安装和配置。

下面是postgersSQL的使用,在使用postgres的时候要在postgres用户下使用。

createdb mydb

执行这句的时候回出现很多问题,具体见:doc
下面是常见的一种错误:

createdb: could not connect to database postgres: FATAL: role “joe” does not exist
这个是因为没有以postgres用户执行的原因。

执行SQL语句:

psql mydb

之后就可以像普通关系型数据库一样执行SQL语句了,具体参看doc

posted @ 2018-03-15 22:20  jimcsharp  阅读(7495)  评论(1编辑  收藏  举报