Linux下的PostgreSQL安装

1. 安装环境

postgresql-11.1

CentOS-6.3

确认linux系统可以正常连接网络,因为在后面需要添加依赖包。

2. pg数据库下载地址

http://www.postgresql.org/ftp/source/

3. 安装依赖包

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

4. 安装postgres

1)在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。

[root@localhost pgsql]# ls
postgresql-11.1.tar.gz
[root@localhost pgsql]# pwd
//pgsql

2)解压压缩包

[root@localhost pgsql]# tar -zxvf postgresql-11.1.tar.gz 

3)进入解压后的文件夹 

[root@localhost pgsql]# cd postgresql-11.1
[root@localhost postgresql-11.1]# ls
aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
config      configure.in  COPYRIGHT  GNUmakefile.in  INSTALL  README

4)编译postgresql源码

[root@localhost postgresql-11.1]# ./configure --prefix=/pgsql/postgresql

             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@localhost postgresql-11.1]# make

[root@localhost postgresql-11.1]# make install

至此,已完成postgreql的安装。进入/pgsql/postgresql目录可以看到安装后的postgresql的文件。

[root@localhost postgresql]# ls
bin  include  lib  share

5. 创建用户组postgres并创建用户postgres 

[root@localhost postgresql-11.1]# groupadd postgres
[root@localhost postgresql-11.1]# useradd -g postgres postgres
[root@localhost postgresql-11.1]# id postgres
uid=501(postgres) gid=501(postgres) 组=501(postgres)

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

这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql/postgresql/data目录下:

[root@localhost postgresql-11.1]# cd /pgsql/postgresql
[root@localhost postgresql]# mkdir data
[root@localhost postgresql]# chown postgres:postgres data
[root@localhost 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

7. 配置环境变量

进入home/postgres目录可以看到.bash_profile文件。

[root@localhost postgresql]# cd /home/postgres
[root@localhost 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@localhost postgres]# 

编辑修改.bash_profile文件。

[root@localhost postgres]# vi .bash_profile 

添加以下内容。

export PGHOME=/pgsql/postgresql

export PGDATA=/pgsql/postgresql/data

PATH=$PATH:$HOME/bin:$PGHOME/bin

保存,退出vi。执行以下命令,使环境变量生效

[root@localhost postgres]# source .bash_profile 

8. 切换用户到postgres并使用initdb初使用化数据库

[root@localhost postgres]# su - postgres
[postgres@localhost ~]$ 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@localhost //]$ cd /pgsql/postgresql/data
[postgres@localhost 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

9.  配置服务

修改/pgsql/postgresql/data目录下的两个文件。

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

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

[postgres@localhost 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)

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

 [postgres@localhost 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

10. 设置PostgreSQL开机自启动

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

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

[postgres@localhost pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts
[postgres@localhost start-scripts]$ ls
freebsd  linux  macos

1)切换为root用户,修改linux文件属性,添加X属性

[root@localhost start-scripts]# chmod a+x linux

2) 复制linux文件到/etc/init.d目录下,更名为postgresql

[root@localhost 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@localhost init.d]# chkconfig --add postgresql

查看开机自启动服务设置成功。

[root@localhost init.d]# chkconfig

postgresql         0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

5)编辑/etc/sysconfig/iptables文件开放5432端口。

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

-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT

重启服务
[root@localhost sysconfig]# /etc/init.d/iptables restart
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter           [确定]
iptables:正在卸载模块:                                     [确定]
iptables:应用防火墙规则:                                 [确定]

查看端口是否开放

[root@localhost 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@localhost init.d]# service postgresql start
Starting PostgreSQL: ok

查看PostgreSQL服务
[root@localhost 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

现在就可以通过客户端进行连接访问数据库啦! 


https://www.cnblogs.com/sybblogs/p/5717392.html


 



 

猜你喜欢

转载自blog.csdn.net/min0324/article/details/85019434