Yum安裝postgresql
yum 安裝
如果是默認yum安裝的話,會安裝較低版本的PostgreSQL8.4。
我們使用PostgreSQLYum Repository 來安裝最新版本的PostgreSQL。
1.安裝PostgreSQLyum repository
[root@tigase3~]# rpm -i http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-centos92-9.2-6.noarch.rpm
1.1 安裝新版本PostgreSQL
[root@tigase3~]# yum install postgresql92-server postgresql92-contrib
1.2 查看安裝
[root@tigase3 ~]# rpm-qa | grep postgresql
postgresql92-9.2.8-1PGDG.rhel6.x86_64
postgresql92-server-9.2.8-1PGDG.rhel6.x86_64
postgresql92-libs-9.2.8-1PGDG.rhel6.x86_64
postgresql92-contrib-9.2.8-1PGDG.rhel6.x86_64
2.初始化並啓動數據庫
[root@tigase3~]# /etc/init.d/postgresql-9.2 initdb
Initializingdatabase: [ OK ]
3.啓動數據庫並測試
[root@tigase3~]# chkconfig postgresql-9.2 on
[root@tigase3~]# service postgresql-9.2 start
Startingpostgresql-9.2 service: [ OK ]
[root@tigase3~]# su – postgres #切到postgres用戶
-bash-4.1$psql
psql(9.2.8)
Type"help" for help.
postgres=#
在未設置第7步pg_hba.conf時只能切換到postgres登錄,更改爲以下後,可以直接在root下psql -U postgres
local all all trust
4.設置postgresql數據庫超級用戶的密碼,默認超級用戶爲postgres,默認密碼爲空,設定超級用戶密碼爲postgres.
postgres=#ALTER USER postgres WITH PASSWORD 'postgres';
ALTERROLE
5、創建一個用戶tigase,密碼爲tigase.同時創建一個數據庫tigasedb,數據庫的所有者爲tigase.
postgres=#create user tigase with password 'tigase';
CREATEROLE
postgres=#create database tigasedb with owner tigase;
CREATEDATABASE
postgres=#\l #使用\l列出所建數據庫及所有者
Listof databases
Name | Owner | Encoding | Collate | Ctype | Accessprivileg
es
tigasedb | tigase | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres=#\q #使用\q退出
-bash-4.1$
6、修改PostgresSQL數據庫配置實現遠程訪問。
-bash-4.1$vi /var/lib/pgsql/9.2/data/postgresql.conf
#listen_addresses= ‘localhost’ 改爲:listen_addresses= '*' #監聽整個網絡
7、修改客戶端認證配置文件pg_hba.conf,將需要遠程訪問數據庫的IP地址或地址段加入該文件。
-bash-4.1$vi /var/lib/pgsql/9.2/data/pg_hba.conf
#IPv4 local connections:
local all all trust
host all all 127.0.0.1/32 trust
host all all 192.168.3.0/24 trust
添加遠程訪問數據庫的IP網段爲trust,如果爲MD5;訪問數據庫時需要輸入密碼。
8、重啓服務,測試連接數據庫。
[root@tigase3~]# service postgresql-9.2 restart
Stoppingpostgresql-9.2 service: [ OK ]
Startingpostgresql-9.2 service: [ OK ]
[root@tigase3~]# psql -U postgres或su – postgres #切到postgres用戶
-bash-4.1$psql -h 127.0.0.1 -U tigase -W -d tigasedb # -W強行要求用戶輸入密碼
Passwordfor user tigase:
psql(9.2.8)
Type"help" for help.
tigasedb=> #已進入tigasedb數據庫,可使用\dt、\d查詢表和表結構,沒有數據會顯示關係不能找到。
以下爲安裝tigase服務器時的表操作
更改數據庫表的所有者。
-bash-4.1$psql -h 127.0.0.1 -U postgres -W -d tigasedb #進入超級用戶更改tigasedb
Passwordfor user postgres:
psql(9.2.8)
Type"help" for help.
tigasedb=#
tigasedb=#alter table short_news owner to tigase; #更改表short_news的所有者爲tigase.
ALTERTABLE
tigasedb=#\dt
Schema| Name | Type | Owner
--------+--------------------------+-------+----------
public| short_news | table | tigase
public| tig_nodes | table | postgres