PG 10.5源码下载地址为:
https://www.postgresql.org/ftp/source/v10.5/
操作系统安装完成后,修改安全设置
root@rhel7x ~]# systemctl stop firewalld.service
[root@rhel7x ~]# systemctl disable firewalld.service
Removed symlink/etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@rhel7x ~]# vi /etc/selinux/config
[root@rhel7x ~]# cat /etc/selinux/config
# This file controls the state ofSELinux on the system.
# SELINUX= can take one of these threevalues:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of threetwo values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes areprotected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
将rhel7.5光盘加载到操作系统,然后执行:
cp -r /run/media/root/RHEL-7.5\ Server.x86_64/* /mnt/rhel7
配置yum:
[root@pg10 ~]# cat /etc/yum.repos.d/yumlocal.repo
[rhel7]
name=rhel7
baseurl=file:///mnt/rhel7
enabled=1
gpgcheck=1
gpgkey=file:///mnt/rhel7/RPM-GPG-KEY-redhat-release
安装依赖包:
yum -y install perl*
yum -y install *readline*
yum -y install *zlib*
yum -y install *Python*
准备工作:
创建用户
#创建用户和组
groupadd postgres
useradd -g postgres postgres
准备安装目录
#进入安装目录
cd /usr/local/
#删除原有安装(如果有的话)
rm -rf postgres*
安装:
#配置
cd postgresql-10.5
./configure --prefix=/usr/local/postgresql --with-perl --with-python
#编译安装
make
make install
#安装contrib目录下的一些工具,是第三方组织的一些工具代码,建议安装
cd contrib
make && make install
环境变量:
vi /etc/profile
export PATH=/usr/local/postgresql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/postgresql/lib:$LD_LIBRARY_PATH
数据库存放位置:
[root@pg96 contrib]# mkdir -p /pgdata
授权给psotgres账户:
chown -R postgres.postgres /pgdata
chown -R postgres:postgres /usr/local/postgresql
切换用户到postgres:
设置环境变量:
export PGHOME=/usr/local/postgresql
export PGDATA=/pgdata
[postgres@pg10 ~]$ vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PGHOME=/usr/local/postgresql
export PGDATA=/pgdata
export PATH
#初始化数据库
initdb -D /pgdata
#启动服务
pg_ctl -D /pgdata -l /pgdata/logfile start
[postgres@pg96 ~]$ pg_ctl -D /pgdata -l /pgdata/logfile start
server starting
启停数据库:
启动服务:
pg_ctl start -D /pgdata -l /pgdata/logfile
#重启服务
pg_ctl restart -D /pgdata -l /pgdata/logfile
#停止服务
pg_ctl stop -D /pgdata -l /pgdata/logfile
停止服务,还可以加上-m参数,后面三个选项:
smart:正常关闭,类似Oracle normal
fast:快速关闭,类似Oracle的immediate
immediate:立即关闭,类似Oracle的abort
#登录客户端使用"psql 数据库名"登录数据库。
#缺省数据库名时,连接到默认的数据库postgres。
#本地不用指定数据库
psql
#创建测试数据库
create database micky;
#切换到micky 数据库
\c micky
#创建测试表
create table test (id integer, name text);
#插入测试数据
insert into test values (1,'micky');
#选择数据
select * from test ;
[postgres@pg10 ~]$ psql
psql (10.5)
Type "help" for help.
postgres=# create database micky;
CREATE DATABASE
postgres=# \c micky
You are now connected to database "micky" as user "postgres".
micky=# create table test (id integer, name text);
CREATE TABLE
micky=# insert into test values (1,'micky');
INSERT 0 1
micky=# select * from test ;
id | name
----+-------
1 | micky
(1 row)
micky=#
micky=# \q
[postgres@pg96 ~]$
#修改postgresql.conf 文件
#--------------------允许远程连接---------------------------
#修改客户端认证配置文件pg_hba.conf,将需要远程访问数据库的IP地址或地址段加入该文件
vi /pgdata/pg_hba.conf
#在文件的最下方加上下面的这句话(出于安全考虑,不建议这样配置)
host all all 0.0.0.0/0 trust
#设置监听整个网络,查找“listen_addresses ”字符串,
vi /pgdata/postgresql.conf
#修改为如下:
listen_addresses = '*'
port = 5432
重启服务,配置生效:
pg_ctl restart -D /pgdata -l /pgdata/logfile