CentOS 6.6 yum 安裝PostgreSQL 9.4以及常用設置

今天,也是心血業潮,也是想著為了弄一個POSTGRESQL的測試運行環境,就想著在VMWARE的CENTOS下安裝一個POSTGRESQL資料庫,方便日後技術研究。下面為安裝過程。

一、引入 PostgreSQL 9.4 Repository 官方的安裝源

rpm -Uvh http://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-6.6-x86_64/pgdg-centos94-9.4-2.noarch.rpm

二、安裝PostgreSQL 9.4

yum install postgresql94 postgresql94-server postgresql94-contrib

三、初始化PostgreSQL資料庫

service postgresql-9.4 initdb

四、設置開機自啟動

service postgresql-9.4 start

chkconfig postgresql-9.4 on

五、修改監聽地址、埠

/var/lib/pgsql/9.4/data/postgresql.conf 中 把注釋符號去掉,顯示如下:

listen_address=’*’

port=5432

六、增加操作系統用戶

[root@wc1 ~]# adduser pgdbuser

七、創建postgresql資料庫中的用戶

在安裝完postgresql資料庫之後,會有一個默認的用戶postgres,切換到postgres用戶,然後執行psql命令,連接postgresql,然後再創建pgdbuser用戶,最後退出

[root@wc1 ~]# su – postgres

-bash-4.1$ psql

psql (9.4.11)

Type “help” for help.                                        ^

postgres=# create user pgdbuser  with superuser login password ‘pgdbuser’;

CREATE ROLE

postgres=# \q

八、修改/var/lib/pgsql/9.4/data/pg_hba.conf文件如下:

其中ident是linux系統下postgresql默認的local認證方式,通過操作系統用戶映射的資料庫用戶,不需要輸入密碼,就可以登錄。

而md5,不需要在操作系統層建立對應的同名的用戶。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# “local” is for Unix domain socket connections only

local   all             all                                     md5

# IPv4 local connections:

host    all             all             127.0.0.1/32            md5

# IPv6 local connections:

host    all             all             ::1/128                 md5

修改之後要重啟服務:

[root@wc1 Desktop]# service postgresql-9.4 restart

Stopping postgresql-9.4 service:                           [  OK  ]

Starting postgresql-9.4 service:                           [  OK  ]

九、登錄postgresql

由於都是需要密碼驗證登錄,所以當前os用戶為root,需要指定登錄的資料庫、用戶,然後按照提示輸入密碼後登陸。

接下來,創建資料庫,顯示當前資料庫,切換到新建的庫,創建表,插入數據,查詢數據。

[root@wc1 Desktop]# psql -d postgres -U pgdbuser

Password for user pgdbuser:

psql (9.4.11)

Type “help” for help.

 

postgres=# create database test;

ERROR:  database “test” already exists

postgres=# create database xxx;

CREATE DATABASE

postgres=# select current_database();

current_database

——————

postgres

(1 row)

 

postgres=# \c xxx

You are now connected to database “xxx” as user “pgdbuser”.

xxx=# create table tb(id int);

CREATE TABLE

xxx=# insert into tb values(1);

INSERT 0 1

xxx=# select * from tb;

id

—-

1

(1 row)

 

xxx=#

十、遇到的問題解決:沒有用於主機「192.168.179.38」,用戶「postgres」,資料庫「postgres」

這個錯誤的發生是因為在修改pg_hba.conf文件時,沒有將遠程機的IP地址添加到裡面。帖子里加了行是:

host  all  all  10.0.0.0/8  md5

將這行的IP地址換成  0.0.0.0/0  就可以實現任何別的IP地址的遠程機訪問伺服器的功能.然後在WINDOWS 用pgadmin軟體也可以正常登陸啦。

以下文章點擊率最高

Loading…

     

如果這文章對你有幫助,請掃左上角微信支付-支付寶,給於打賞,以助博客運營