目录

一、安装环境

二、pg数据库安装包下载

三、安装依赖包

四、安装postgres

五、创建用户组postgres并创建用户postgres 

六、创建postgresql数据库的数据主目录并修改文件所有者

七、配置环境变量

八、切换用户到postgres并使用initdb初使用化数据库

九、配置服务

十、设置PostgreSQL开机自启动

十一、开始测试

一、安装环境
postgresql-11.1
CentOS-6.4
注意:确认linux系统可以正常连接网络,因为在后面需要添加依赖包。
二、pg数据库安装包下载
下载地址:http://www.postgresql.org/ftp/source/

选择要安装的版本进行下载:

三、安装依赖包
在要安装postgresql数据库的Linux服务器(hostname:weekend02)上执行以下命令安装所需要的依赖包:

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

四、安装postgres
1、在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。

[root@weekend02 pgsql]# ls
postgresql-11.1.tar.gz
[root@weekend02 pgsql]# pwd
//pgsql
2、解压压缩包

[root@weekend02 pgsql]# tar -zxvf postgresql-11.1.tar.gz 
3、进入解压后的文件夹 

[root@weekend02 pgsql]# cd postgresql-11.1
[root@weekend02 postgresql-11.1]# ls
aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
config      configure.in  COPYRIGHT  GNUmakefile.in  INSTALL  README
4、编译postgresql源码

[root@weekend02 postgresql-11.1]# ./configure --prefix=/pgsql/postgresql
选项    描述
–prefix=prefix    安装到prefix指向的目录;默认为/usr/local/pgsql
–bindir=dir    安装应用程序到dir;默认为prefix/bin
–with-docdir=dir    安装文档到dir;默认为prefix/doc
–with-pgport=port    设置默认的服务器端网络连接服务TCP端口号
–with-tcl    为服务端提供Tcl存储过程支持
–with-perl    为服务端提供Perl存储过程支持
–with-python    为服务端提供Python存储过程支持
[root@weekend02 postgresql-11.1]# make
[root@weekend02 postgresql-11.1]# make install
至此,已完成postgreql的安装。进入/pgsql/postgresql目录可以看到安装后的postgresql的文件。

[root@weekend02 postgresql]# ls
bin  include  lib  share
五、创建用户组postgres并创建用户postgres 
[root@weekend02 postgresql-11.1]# groupadd postgres
[root@weekend02 postgresql-11.1]# useradd -g postgres postgres
[root@weekend02 postgresql-11.1]# id postgres
uid=501(postgres) gid=501(postgres) 组=501(postgres)
六、创建postgresql数据库的数据主目录并修改文件所有者
这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql/postgresql/data目录下:

[root@weekend02 postgresql-11.1]# cd /pgsql/postgresql
[root@weekend02 postgresql]# mkdir data
[root@weekend02 postgresql]# chown postgres:postgres data
[root@weekend02 postgresql]# ls -al
总用量 28
drwxr-xr-x. 7 root     root     4096 12月 15 23:39 .
drwxr-xr-x. 4 root     root     4096 12月 15 23:14 ..
drwxr-xr-x. 2 root     root     4096 12月 15 23:14 bin
drwxr-xr-x. 2 postgres postgres 4096 12月 15 23:39 data
drwxr-xr-x. 6 root     root     4096 12月 15 23:14 include
drwxr-xr-x. 4 root     root     4096 12月 15 23:14 lib
drwxr-xr-x. 6 root     root     4096 12月 15 23:14 share
七、配置环境变量
进入home/postgres目录可以看到.bash_profile文件。

[root@weekend02 postgresql]# cd /home/postgres
[root@weekend02 postgres]# ls -al
总用量 28
drwx------. 4 postgres postgres 4096 12月 15 23:37 .
drwxr-xr-x. 5 root     root     4096 12月 15 23:19 ..
-rw-r--r--. 1 postgres postgres   18 5月  11 2012 .bash_logout
-rw-r--r--. 1 postgres postgres  178 12月 15 23:37 .bash_profile
-rw-r--r--. 1 postgres postgres  124 5月  11 2012 .bashrc
drwxr-xr-x. 2 postgres postgres 4096 11月 12 2010 .gnome2
drwxr-xr-x. 4 postgres postgres 4096 9月  29 05:12 .mozilla
[root@weekend02 postgres]# 
编辑修改.bash_profile文件。

[root@weekend02 postgres]# vi .bash_profile 
添加以下内容。

export PGHOME=/pgsql/postgresql
 
export PGDATA=/pgsql/postgresql/data
 
PATH=$PATH:$HOME/bin:$PGHOME/bin
保存,退出vi。执行以下命令,使环境变量生效

[root@weekend02 postgres]# source .bash_profile 
八、切换用户到postgres并使用initdb初使用化数据库
[root@weekend02 postgres]# su - postgres
 
[postgres@weekend02 ~]$ initdb
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 "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
 
Data page checksums are disabled.
 
fixing permissions on existing directory /pgsql/postgresql/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:
 
    pg_ctl -D /pgsql/postgresql/data -l logfile start
可以看到 /pgsql/postgresql/data已经有文件了。

[postgres@weekend02 /]$ cd /pgsql/postgresql/data
 
[postgres@weekend02 data]$ ls
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact
九、配置服务
修改/pgsql/postgresql/data目录下的两个文件。

postgresql.conf   配置PostgreSQL数据库服务器的相应的参数。  

pg_hba.conf        配置对数据库的访问权限。

[postgres@weekend02 data]$ vi postgresql.conf 
 
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)
其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。

[postgres@weekend02 data]$ vi pg_hba.conf 
找到最下面这一行 ,这样局域网的人才能访问。红色为新添加内容。

# IPv4 local connections:
host    all             all             0.0.0.0/0                  trust
host    all             all             127.0.0.1/32            trust

十、设置PostgreSQL开机自启动
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

linux文件即为linux系统上的启动脚本

[postgres@weekend02 pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts
[postgres@weekend02 start-scripts]$ ls
freebsd  linux  macos
1)切换为root用户,修改linux文件属性,添加X属性

[root@weekend02 start-scripts]# chmod a+x linux
2) 复制linux文件到/etc/init.d目录下,更名为postgresql

[root@weekend02 start-scripts]# cp linux /etc/init.d/postgresql
3)修改/etc/init.d/postgresql文件的两个变量

prefix设置为postgresql的安装路径:/pgsql/postgresql

PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data

4)设置postgresql服务开机自启动

[root@weekend02 init.d]# chkconfig --add postgresql
查看开机自启动服务设置成功。

[root@weekend02 init.d]# chkconfig
 
postgresql         0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
5)编辑/etc/sysconfig/iptables文件开放5432端口。

[root@weekend02 sysconfig]# cd /etc/sysconfig
[root@weekend02 sysconfig]# vi iptables
添加以下内容

-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT
重启服务

[root@weekend02 sysconfig]# /etc/init.d/iptables restart
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter           [确定]
iptables:正在卸载模块:                                     [确定]
iptables:应用防火墙规则:                                 [确定]
查看端口是否开放

[root@weekend02 sysconfig]# /sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5432 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   
6)执行service postgresql start,启动PostgreSQL服务

[root@weekend02 init.d]# service postgresql start
Starting PostgreSQL: ok
查看PostgreSQL服务

[root@weekend02 init.d]# ps -ef | grep postgres
root      12040   3014  0 Dec15 pts/0    00:00:00 su - postgres
postgres  12041  12040  0 Dec15 pts/0    00:00:00 -bash
postgres  12177      1  0 00:29 ?        00:00:00 /pgsql/postgresql/bin/postmaster -D /pgsql/postgresql/data
postgres  12179  12177  0 00:29 ?        00:00:00 postgres: checkpointer                                    
postgres  12180  12177  0 00:29 ?        00:00:00 postgres: background writer                               
postgres  12181  12177  0 00:29 ?        00:00:00 postgres: walwriter                                       
postgres  12182  12177  0 00:29 ?        00:00:00 postgres: autovacuum launcher                             
postgres  12183  12177  0 00:29 ?        00:00:00 postgres: stats collector                                 
postgres  12184  12177  0 00:29 ?        00:00:00 postgres: logical replication launcher                    
root      12198  12132  0 00:30 pts/0    00:00:00 grep postgres
十一、开始测试
切换为postgres用户,进入客户端:

$ su - postgres
$ psql
创建数据库用户

赋予账号权限

新建数据库
退出

[postgres@weekend02 ~]$ psql 
psql (11.1)
Type "help" for help.
 
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)
 
postgres=# create user pg password 'pg';
CREATE ROLE
postgres=# ALTER ROLE pg SUPERUSER;
ALTER ROLE
postgres=# create database pg;
CREATE DATABASE
postgres=# \q
重新登录数据库
输入密码
显示数据库

[postgres@weekend02 ~]$ psql -U pg -d pg -h weekend02
psql (11.1)
Type "help" for help.
 
pg=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 pg        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)
 
pg=# 
说明数据库安装成功。
————————————————
版权声明:本文为CSDN博主「皮哥四月红」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43230682/article/details/108403642

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐