redhat6.7在线安装postgresql9

原文:http://wandejun1012.iteye.com/blog/2015777

1、安装postgresql9.0 yum 仓库

rpm -i http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-redhat92-9.2-8.noarch.rpm

注意,以上的rpm包有可能已经有变更,如果找不到可以先到 http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/里面去查看

2、安装postgresql9.2

yum install postgresql92-server postgresql92-contrib

3、初始化数据库

[root@host-172-16-80-173 ~]# /etc/init.d/postgresql-9.2 initdb
Initializing database:                                     [  OK  ]

4、启动数据库

注意:postgresql启动后就可以利用service postgresql-9.2 start/restart/stop来控制它了。

虽然打完service后,按p不提示postgresql-9.2,但是可以用手输。

5.把postgresql加入自启动列表

cd /etc/init.d

chkconfig --add postgresql-9.2

chkconfig postgresql-9.2 on

6.查看一下自启动列表

chkconfig --list

在这里可以看到postgresql已经在其中了。

portreserve        0:off    1:off    2:on    3:on    4:on    5:on    6:off
postfix            0:off    1:off    2:on    3:on    4:on    5:on    6:off
postgresql-9.2     0:off    1:off    2:on    3:on    4:on    5:on    6:off

7.PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’

[root@host-172-16-80-173 init.d]# su - postgres
-bash-4.1$ 

然后:

-bash-4.1$ psql
psql (9.2.23)
Type "help" for help.

postgres=# 

修改密码:

postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=# select * from pg_shadow ;
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |               passwd                | valuntil | useconfig 
----------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+-----------
 postgres |       10 | t           | t        | t         | t       | md53175bce1d3201d16594cebf9d7eb3f9d |          | 
(1 row)

8、创建一个数据库

postgres=# create database test;
CREATE DATABASE

9、列举数据库

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
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

10、设置postgresql可以远程访问

[root@host-172-16-80-173 ~]# find / -name postgresql.conf
/var/lib/pgsql/9.2/data/postgresql.conf
[root@host-172-16-80-173 ~]# vim /var/lib/pgsql/9.2/data/postgresql.conf

将listen_addresses = 'localhost'的#号去掉,并改成listen_addresses = '*'

vim /var/lib/pgsql/9.2/data/pg_hba.conf

增加下面红色的一行

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             0.0.0.0/0               md5
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

pg_hba.conf,位置与postgresql.conf相同,虽然上面配置允许任意地址连接PostgreSQL,但是这在pg中还不够,我们还需在pg_hba.conf中配置服务端允许的认证方式。

默认pg只允许本机通过密码认证登录,修改为上面内容后即可以对任意IP访问进行密码验证。

重启数据库

[root@host-172-16-80-173 ~]# /etc/init.d/postgresql-9.2 restart
Stopping postgresql-9.2 service:                           [  OK  ]
Starting postgresql-9.2 service:                           [  OK  ]

验证远程登录:

[root@host-172-16-80-173 ~]# su - postgres
-bash-4.1$ psql -h 172.16.80.173 -U postgres -d test 
Password for user postgres: 
psql (9.2.23)
Type "help" for help.

test=# 

11、安装完毕

原文地址:https://www.cnblogs.com/boshen-hzb/p/7488327.html