postgresql安裝教程

2021-07-11 01:13:42 字數 4121 閱讀 3924

pg相對於其他分布式的資料庫如greenplum,vertica的安裝已經是很簡單了。今天安裝postgresql了,那就順便把安裝過程記錄下吧~

安裝環境說明:

安裝版本:postgresql9.4

安裝系統環境:centos6.5

安裝:

根據官網的介紹(

1.首先更新yum源:

yum install

2.安裝postgresql

yum install postgresql94-server postgresql94-contrib

3.簡單設定一下,先初始化資料庫,再開啟服務,設定開機啟動

service postgresql-9.4 initdb

/etc/init.d/postgresql-9.4 start

chkconfig postgresql-9.4 on

到此,postgresql已經安裝完成了。

配置:

一、新增新使用者和新資料庫

初次安裝後,預設生成乙個名為postgres的資料庫和乙個名為postgres的資料庫使用者。這裡需要注意的是,同時還生成了乙個名為postgres的linux系統使用者。

下面,我們使用postgres使用者,來生成其他使用者和新資料庫。好幾種方法可以達到這個目的,我這裡就介紹一種。

第一種方法,使用postgresql控制台。

首先,新建乙個linux新使用者,可以取你想要的名字,這裡為dbuser。

adduser dbuser

passwd dbuser

然後,切換到postgres使用者。
su - postgres
下一步,使用psql命令登入postgresql控制台。

psql

這時相當於系統使用者postgres以同名資料庫使用者的身份,登入資料庫,這是不用輸入密碼的。如果一切正常,系統提示符會變為"postgres=#",表示這時已經進入了資料庫控制台。以下的命令都在控制台內完成。

第一件事是使用\password命令,為postgres使用者設定乙個密碼。(注意這裡設定的是資料庫pg裡postgres使用者的密碼,如果你需要設定linux使用者postgres的密碼那就用 passwd postgres,這兩個密碼還是不同的哦~)

\password postgres

第二件事是建立資料庫使用者dbuser(剛才建立的是linux系統使用者),並設定密碼。

create user dbuser with password 'password';

第三件事是建立使用者資料庫,這裡為exampledb,並指定所有者為dbuser。

create database exampledb owner dbuser;

第四件事是將exampledb資料庫的所有許可權都賦予dbuser,否則dbuser只能登入控制台,沒有任何資料庫操作許可權。

grant all privileges on database exampledb to dbuser;

最後,使用\q命令退出控制台(也可以直接按ctrl+d)。

\q

到此,使用者已經建立了而且有了他自己的資料庫,那我們去登入試試~

psql -u dbuser -d exampledb -h 127.0.0.1 -p 5432

如果輸入密碼以後發現報錯!!

psql: 致命錯誤:  對使用者"dbuser"的對等認證失敗

這時候,我們就需要修改下pg的配置檔案了:

這牽涉到pg的認證方式了。我們知道pg的認證方式是儲存在配置檔案 pg_hba.conf 中,這個檔案在初始化資料(initdb) 所制定的目錄,預設位址是 /var/lib/pgsql/9.4/data/pg_hba.conf ,這是乙個比較標準的linux風格的配置檔案,一行代表一條規則。主要內容如下:

# type  database        user            address                 method

# "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

我們關心的是最後那個字段,method,就是認證的方式,有很多種方式可以配置,常用的

trust 任何連線都允許,不需要密碼

reject 拒絕符合條件(前面幾個條件)的請求

md5 接收乙個md5加密過的密碼

password 接收乙個密碼來登陸,只在可信的網路使用這種方式

gss 使用gssapi認證,只在tcp/ip連線可用

sspi 只在windows可用的一種方式

krb5 不常用,只在tcp/ip可用

ident 使用作業系統使用者名稱認證,驗證它是否符合請求的的資料庫使用者名稱

ldap 使用ldap伺服器認證

cert 使用ssl客戶端認證

pam 使用作業系統的pam模組服務

根據上面的報錯,是配置檔案使用了ident認證方式。所以我們修改

host    all             all             127.0.0.1/32            ident

為host    all             all             127.0.0.1/32            password

之後重啟服務:

/etc/init.d/postgresql-9.4 restart

psql -u dbuser -d exampledb -h 127.0.0.1 -p 5432

再次登入,就成功了!

pgadmin連線postgresql:

2.登入時候如果報錯或者一直沒反應,那麼有以下幾種情況:

1)你的linux虛擬機器沒關閉防火牆,嘗試關掉防火牆看看:

setenforce 0

/etc/init.d/iptables stop

2)如果報錯說沒有監聽,那麼你需要修改下pg的配置檔案:

1.

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

將改檔案裡的

host all all 127.0.0.1/32 ident

改為host all all 0.0.0.0/0 password

表示所有的ip都能連線改伺服器。

2.

vim /var/lib/pgsql/9.4/data/postgresql.conf

修改listen_addresses = '*' //監聽所有ip的連線,預設是本機

port = 5432 //這個不開也行,預設就是5432埠

到此應該都能完全連線上了。

PostgreSQL安裝入門教程

一 安裝 首先,安裝postgresql客戶端。sudo apt get install postgresql client 然後,安裝postgresql伺服器。sudo apt get install postgresql 正常情況下,安裝完成後,postgresql伺服器會自動在本機的5432...

PostgreSQL新手教程

自從mysql被oracle收購以後,postgresql逐漸成為開源關係型資料庫的首選。本文介紹postgresql的安裝和基本用法,供初次使用者上手。以下內容基於debian作業系統,其他作業系統實在沒有精力兼顧,但是大部分內容應該普遍適用。1首先,安裝postgresql客戶端。sudo ap...

postgresql簡易教程

ubuntu安裝postgresql 使用apt get 安裝postgresql sudo apt get update sudo apt get install postgresql postgresql client安裝完畢後,系統會自動建立乙個資料庫postgres,密碼為空 sudo i ...