目录
一、安装环境
二、pg数据库安装包下载
三、安装依赖包
四、安装postgres
五、创建用户组postgres并创建用户postgres
六、创建postgresql数据库的数据主目录并修改文件所有者
七、配置环境变量
八、切换用户到postgres并使用initdb初使用化数据库
九、配置服务
十、设置PostgreSQL开机自启动
十一、开始测试
下载地址: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
1、在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。
[root@weekend02 pgsql]# ls postgresql-11.1.tar.gz [root@weekend02 pgsql]# pwd //pgsql2、解压压缩包
[root@weekend02 pgsql]# tar -zxvf postgresql-11.1.tar.gz3、进入解压后的文件夹
[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 README4、编译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这个数据库主目录是随实际情况而不同,这里我们的主目录是在/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可以看到 /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源码目录的contrib/start-scripts路径下。
linux文件即为linux系统上的启动脚本
[postgres@weekend02 pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts [postgres@weekend02 start-scripts]$ ls freebsd linux macos1)切换为root用户,修改linux文件属性,添加X属性
[root@weekend02 start-scripts]# chmod a+x linux2) 复制linux文件到/etc/init.d目录下,更名为postgresql
[root@weekend02 start-scripts]# cp linux /etc/init.d/postgresql3)修改/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 destination6)执行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=#说明数据库安装成功。
