案例环境:
虚拟机:CenterOS 7.0.1406(注意:centerOS自带postgres9.2的版本安装包)
PG版本:postgresql-10.5-1-linux-x64-binaries.tar.gz
redis_fwd版本:redis_fdw-REL_10_STABLE.zip
redis版本:redis-4.0.11.tar.gz
Hiredis (这里最好直接拷贝redis解压目录deps下的Hiredis)
PG下载地址:https://www.enterprisedb.com/download-postgresql-binaries
redis_fwd下载地址:https://github.com/pg-redis-fdw/redis_fdw/branches
redis下载地址:https://redis.io/download
虚拟机下载地址:http://archive.kernel.org/centos-vault/7.0.1406/isos/x86_64/
安装虚拟机以及redis的过程 这里省略。
安装PG
CenterOS是自带PG的安装版本的 使用 yum list postgres*可以查看安装了包 和可用的包
[root@localhost /]# yum list postgres*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Available Packages
postgresql.i686 9.2.24-1.el7_5 updates
postgresql.x86_64 9.2.24-1.el7_5 updates
postgresql-contrib.x86_64 9.2.24-1.el7_5 updates
postgresql-devel.i686 9.2.24-1.el7_5 updates
postgresql-devel.x86_64 9.2.24-1.el7_5 updates
postgresql-docs.x86_64 9.2.24-1.el7_5 updates
postgresql-jdbc.noarch 9.2.1002-6.el7_5 updates
postgresql-jdbc-javadoc.noarch 9.2.1002-6.el7_5 updates
postgresql-libs.i686 9.2.24-1.el7_5 updates
postgresql-libs.x86_64 9.2.24-1.el7_5 updates
postgresql-odbc.x86_64 09.03.0100-2.el7 base
postgresql-plperl.x86_64 9.2.24-1.el7_5 updates
postgresql-plpython.x86_64 9.2.24-1.el7_5 updates
postgresql-pltcl.x86_64 9.2.24-1.el7_5 updates
postgresql-server.x86_64 9.2.24-1.el7_5 updates
postgresql-static.i686 9.2.24-1.el7_5 updates
postgresql-static.x86_64 9.2.24-1.el7_5 updates
postgresql-test.x86_64 9.2.24-1.el7_5 updates
postgresql-upgrade.x86_64 9.2.24-1.el7_5 updates
[root@localhost /]# ^C
现在开始解压:
1.postgresql-10.5-1-linux-x64-binaries.tar.gz (解压文件,解压文件目录为pgsql/,目前9.2及以上的都是为免安装版本的。)
lz图片都是用pg10来安装的 (后面会改掉)
[root@localhost admin]# ll
total 144920
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Desktop
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Documents
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Downloads
drwxr-xr-x. 4 root root 4096 Sep 26 19:52 hiredis
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Music
drwxrwxr-x. 9 501 501 97 Aug 8 22:00 pgsql
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Pictures
-rw-r--r--. 1 root root 148357162 Sep 26 19:51 postgresql-10.5-1-linux-x64-binaries.tar.gz
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Public
-rw-r--r--. 1 root root 32028 Sep 26 19:51 redis_fdw-REL_10_STABLE.zip
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Templates
drwxr-xr-x. 2 admin admin 6 Sep 19 22:21 Videos
[root@localhost admin]#
2.使用root用户 su root
[root@localhost /]# su root
3.创建新用户 useradd postgres 设置用户密码:passwd postgres
[root@localhost /]# useradd postgres
[root@localhost /]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost /]#
4.在根目录下创建 mkdir /monchickey 继续创建 mkdir /monchickey/pgsql_data/(pgsql_data 用于初始化目录)
[root@localhost /]# mkdir /monchickey
[root@localhost /]# mkdir /monchickey/pgsql_data
4.1 需要把解压出来的 pgsql移动到monchickey 目录下 :mv pgsql/ /monchickey
[root@localhost /]# mv /home/admin/pgsql/ /monchickey/
5.给postgres用户赋予对monchickey 的操作权限:
[root@localhost /]# chown postgres /monchickey/pgsql_data/
[root@localhost /]# chown postgres /monchickey/
这些都是用root用户进行操作的。
6.切换到postgres用户 su - postgres
[root@localhost /]# su - postgres
7.运行:export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin(运行这句话是因为新创建的用户无论运行什么系统命令都会报 command not found),所以一定要运行。
[postgres@localhost ~]$ export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
8.修改postgres用户的环境变量:vi ~/.bash_profile
PGDATA=/monchickey/pgsql_data 初始化目录
PATH=/monchickey/pgsql 安装目录(记得屏蔽原有的PATH属性) 保存 esc + :wq
fi
# User specific environment and startup programs
#PATH=$PATH:$HOME/.local/bin:$HOME/bin
PGDATA=/monchickey/pgsql_data
PATH=/monchickey/pgsql
export PATH
~
~
~
~
~
~
~
~
~
~
~
9.初始化数据:/monchickey/pgsql/bin/initdb -D /monchickey/pgsql_data/
//在没有初始化之前这个文件夹下是空的
[postgres@localhost ~]$ cd /monchickey/pgsql_data/
[postgres@localhost pgsql_data]$ ll
total 0
//运行了初始化的代码之后就生成了文件(那么他是在哪里指向的呢?)是在我们刚刚为postgres用户配置环境的时候指向了pgdata
[postgres@localhost pgsql_data]$ /monchickey/pgsql/bin/initdb -D /monchickey/pgsql_data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /monchickey/pgsql_data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
[postgres@localhost pgsql_data]$ ll
total 48
drwx------. 5 postgres postgres 38 Sep 26 20:06 base
drwx------. 2 postgres postgres 4096 Sep 26 20:06 global
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_commit_ts
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_dynshmem
-rw-------. 1 postgres postgres 4513 Sep 26 20:06 pg_hba.conf
-rw-------. 1 postgres postgres 1636 Sep 26 20:06 pg_ident.conf
drwx------. 4 postgres postgres 65 Sep 26 20:06 pg_logical
drwx------. 4 postgres postgres 34 Sep 26 20:06 pg_multixact
drwx------. 2 postgres postgres 17 Sep 26 20:06 pg_notify
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_replslot
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_serial
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_snapshots
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_stat
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_stat_tmp
drwx------. 2 postgres postgres 17 Sep 26 20:06 pg_subtrans
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_tblspc
drwx------. 2 postgres postgres 6 Sep 26 20:06 pg_twophase
-rw-------. 1 postgres postgres 3 Sep 26 20:06 PG_VERSION
drwx------. 3 postgres postgres 58 Sep 26 20:06 pg_wal
drwx------. 2 postgres postgres 17 Sep 26 20:06 pg_xact
-rw-------. 1 postgres postgres 88 Sep 26 20:06 postgresql.auto.conf
-rw-------. 1 postgres postgres 22778 Sep 26 20:06 postgresql.conf
[postgres@localhost pgsql_data]$
10:启动数据库:/monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
[postgres@localhost pgsql_data]$ /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
waiting for server to start.... done
server started
[postgres@localhost pgsql_data]$
11:查看数据库是否启动成功:ps -ef | grep postgres 或者查看端口号:lsof -i:5432 或者 netstat -an |grep 5432
[postgres@localhost pgsql_data]$ ps -ef | grep postgres
root 12338 12153 0 20:02 pts/1 00:00:00 su - postgres
postgres 12339 12338 0 20:02 pts/1 00:00:00 -bash
root 12408 12384 0 20:03 pts/1 00:00:00 su - postgres
postgres 12409 12408 0 20:03 pts/1 00:00:00 -bash
postgres 12503 1 0 20:08 pts/1 00:00:00 /monchickey/pgsql/bin/postgres -D /monchickey/pgsql_data
postgres 12505 12503 0 20:08 ? 00:00:00 postgres: checkpointer process
postgres 12506 12503 0 20:08 ? 00:00:00 postgres: writer process
postgres 12507 12503 0 20:08 ? 00:00:00 postgres: wal writer process
postgres 12508 12503 0 20:08 ? 00:00:00 postgres: autovacuum launcher process
postgres 12509 12503 0 20:08 ? 00:00:00 postgres: stats collector process
postgres 12510 12503 0 20:08 ? 00:00:00 postgres: bgworker: logical replication launcher
postgres 12519 12409 0 20:09 pts/1 00:00:00 ps -ef
postgres 12520 12409 0 20:09 pts/1 00:00:00 grep --color=auto postgres
[postgres@localhost pgsql_data]$ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 12503 postgres 3u IPv6 49884 0t0 TCP localhost:postgres (LISTEN)
postgres 12503 postgres 4u IPv4 49885 0t0 TCP localhost:postgres (LISTEN)
12:关闭数据库:/monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ stop
[postgres@localhost pgsql_data]$ /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ stop
waiting for server to shut down.... done
server stopped
[postgres@localhost pgsql_data]$
13:数据库安装成功,但是你使用远程连接PG数据库是 连接不成功
解决方式:去初始化目录 pgsql_data
修改:vi pg_hba.conf 在最后添加一行:host all all 192.168.0.0/16 trust 并且保存
[postgres@localhost pgsql_data]$ vi pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all 192.168.0.0/16 trust
修改 vi postgresql.conf 修改 #listen_addresses = 'localhost' 为 listen_addresses = '*' 在这里加入星号允许访问 并且要去掉注释哦!!
[postgres@localhost pgsql_data]$ vi postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/tmp' # comma-separated list of directories
# (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# (change requires restart)
修改为
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/tmp' # comma-separated list of directories
# (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# (change requires restart)
14.重启postgres服务 /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
[postgres@localhost pgsql_data]$ /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
waiting for server to start.... done
server started
15.防火墙打开端口 这里要切换为root用户哦 sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
sudo firewall-cmd --reload
[postgres@localhost pgsql_data]$ su
Password:
[root@localhost pgsql_data]# sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
success
[root@localhost pgsql_data]# sudo firewall-cmd --reload
success
参考:https://www.cnblogs.com/freeweb/p/8006639.html