發新話題
打印

postgresql資料庫使用1─網友(河馬)

postgresql資料庫使用1─網友(河馬)

使用POSTGRESQL資料庫:
單元一、Linux
作業平台:CentOs:

1)安裝POSTGRESQL資料庫:
[root@localhost ~]# yum install postgres*
.......過程略......................
Transaction Summary
=============================================================================
Install 12 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 10 M
Is this ok [y/N]: y //按下y就會進行安裝

Downloading Packages:

(1/12): postgresql-jdbc-7 100% |=========================| 705 kB 00:00
......................................過程略......................

nstalled: postgresql.i386 0:7.4.13-2.RHEL4.1 postgresql-contrib.i386 0:7.4.13-2.RHEL4.1 postgresql-devel.i386 0:7.4.13-2.RHEL4.1 postgresql-docs.i386 0:7.4.13-2.RHEL4.1 postgresql-jdbc.i386 0:7.4.13-2.RHEL4.1 postgresql-odbc.i386 0:7.3-8.RHEL4.1 postgresql-pl.i386 0:7.4.13-2.RHEL4.1 postgresql-python.i386 0:7.4.13-2.RHEL4.1 postgresql-server.i386 0:7.4.13-2.RHEL4.1 postgresql-tcl.i386 0:7.4.13-2.RHEL4.1 postgresql-test.i386 0:7.4.13-2.RHEL4.1
Dependency Installed: mx.i386 0:2.0.5-3
Complete!
[root@localhost ~]#
安裝完成。

2)初始化資料庫
[root@localhost data]# service postgresql start
初始化資料庫: [ 確定 ]
啟動 postgresql 服務: [ 確定 ]

3)設定及建立POSTGRESQL
[root@localhost data]# pwd
/var/lib/pgsql/data
[root@localhost data]# ls
base pg_clog pg_ident.conf pg_xlog postmaster.opts
global pg_hba.conf PG_VERSION postgresql.conf postmaster.pid


4)設定/var/lib/pgsql/data/pg_hba.conf
local all all ident sameuser
host all all x.x.x.x/32 password
(若資料庫建置於遠端機器(ip:x.x.x.x)記得防火牆要打開設定/var/lib/pgsql/data/postgresql.conf 第30行tcpip_socket = true)

5)重啟資料庫
[root@localhost data]# service postgresql restart
現在PostgreSQL的安裝和設定基本已經完成了,但是目前為止能使用資料庫的本機使用者只有postgres。

6)建立使用者tester:
bash-3.00$ createuser tester -P (-P加上密碼)
Enter password for new user:
Enter it again:
Shall the new user be allowed to create databases? (y/n) n    <==建議一般使用者n/管理人y
Shall the new user be allowed to create more new users? (y/n) n    <==建議都n 除非程式需要
CREATE USER

若建立錯誤,可移除使用者
bash-3.00$ dropuser tester
DROP USER

7)建立資料庫,名字net
bash-3.00$ createdb net
CREATE DATABASE

若建立錯誤,也可移除資料庫
bash-3.00$ dropdb net

有兩個資料表刪不掉的template0、template1
bash-3.00$ dropdb template0
Password:
dropdb: database removal failed: ERROR: cannot drop a template database
會有錯誤
設定本機登入使用密碼,記得先進入設定postgres密碼,才做下面的設定,否則沒設密碼又將下面的密碼認證打開的話,密碼認證會失敗,postgresql是不接受空密碼的,密碼請自改,以下範例123456

net=# alter user postgres with password '123456';
ALTER USER
記得行尾要打上";",打入"\q"跳出。
net=# \q
bash-3.00$ exit
[root@localhost data]#

設定本機登入使用密碼
/var/lib/pgsql/data/pg_hba.conf
local all all password
重啟postgresql
[root@localhost data]# service postgresql restart
停止 postgresql 服務: [ 確定 ]
啟動 postgresql 服務: [ 確定 ]

出現密碼的輸入行了,打入剛才設定的密碼
[root@localhost data]# su postgres
bash-3.00$ psql -d net
密碼:

Welcome to psql 7.4.13, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
net=#
這樣就成功了。

註:如果出現以下的錯誤:表示/var/lib/pgsql/data/pg_hba.conf沒改到
bash-3.00$ psql -d net -U "xxxx"
psql: FATAL: IDENT authentication failed for user "xxxx"

TOP

發新話題