1.准备工作
环境:CentOS 6.8
关闭防火墙
$: service iptables stop # 重启后失效$: chkconfig iptables off # 禁用防火墙
在官方网站下载对应版本tar包,本文使用postgresql 8.2.15版本:postgresql-8.2.15.tar.gz 官方下载网址:
2.安装依赖
$: yum -y install gcc$: yum -y install gcc-c++$: yum -y install readline-devel$: yum -y install zlib-devel
3.开始安装
3.1 解压安装包 安装包放在/usr/local下
$: cd /usr/local$: tar -zvxf postgresql-8.2.15.tar.gz
进入postgresql-8.2.15
$: cd postgresql-8.2.15
3.2 创建Linux “postgres”用户
$: adduser postgres$: passwd postgres # 创建用户密码
3.3 在/usr/local/postgresql-8.2.15 目录下开始安装
$: cd /usr/local/postgresql-8.2.15// 安装依赖$: yum -y install make// 配置$: ./configure --prefix=/usr/local/postgresql// 编译$: make// 安装$: make install
3.4 配置环境变量
$: vi /etc/profile// 在/etc/profile文件的最后一行添加如下内容:PATH=$PATH:/usr/local/postgresql/bin
更新环境变量:
$: source /etc/profile
3.5 初始化数据库
在/usr/local/postgresql目录下初始化数据库:
$: cd /usr/local/postgresql$: mkdir data$: chown postgres:postgres /usr/local/postgresql/data/$: su postgres$: /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
3.6 复制并修改配置文件(修改存放数据目录)
切换到root用户下进行复制及修改配置文件操作
$: su root $: “Password” # 输入root密码//复制安装目录下的linux文件到/etc/init.d/中,并将linux名称重命名为postgresql$: cd /usr/local/postgresql-8.2.15 $: cp /usr/local/postgresql-8.2.15/contrib/start-scripts/linux /etc/init.d/postgresql//编辑复制出来的文件 $: vi /etc/init.d/postgresql//修改以下内容即可# Installation prefixprefix=/usr/local/postgresql# Data directoryPGDATA="/usr/local/postgresql/data"$: chmod +x /etc/init.d/postgresql
3.7 启动数据库和设置开机自启
$: /etc/init.d/postgresql start$: chkconfig postgresql on
3.8 创建数据库操作的历史文件
$: touch /usr/local/postgresql/.pgsql_history$: chown postgres:postgres /usr/local/postgresql/.pgsql_history
3.9 测试数据库是否创建成功,并且连接数据库
$: cd /usr/local/postgresql-8.2.15 $: su postgres$: createdb test$: psql test// \q可以退出Welcome to psql 8.2.15, the PostgreSQL interactive terminal.Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quittest=# \q
4.修改数据库外网访问
4.1 修改数据库外网访问之前先关闭数据库
$: su root $: “Password” # 输入root密码$: /etc/init.d/postgresql stop
4.2 修改pg_hba.conf
$: vi /usr/local/postgresql/data/pg_hba.conf// 找到IPv4 local connections# IPv4 local connections:host all all 127.0.0.1/32 trust// 修改如下蓝色字体参数# IPv4 local connections:host all all 192.168.1.0/24 md5 #根据实际网段填写
4.3 修改postgresql.conf
$: vi /usr/local/postgresql/data/postgresql.conf// 找到listen_addresses:# listen_addresses = 'localhost'// 删除#号,更改如下listen_addresses = '*'
4.4 再次启动postgresql
$: /etc/init.d/postgresql start$: su postgres # 切换用户$: psql -U postgres # 进入交互式// 修改密码,本次密码设置为1234alter user postgres with password '1234'; # \q 可以退出
4.5 安装完成,我们也可输入主机IP,通过以下命令方式进入postgresql
$: psql -h 192.168.1.XXX -d postgres -U postgres -p 5432$: “Password” # 输入刚更改的数据库用户postgres密码1234