Step 1☆ 安裝PostgreSQL
sql
yum install http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm
yum install postgresql93-server postgresql93-contrib
Step 2☆ 開啓防火牆端口
數據庫
iptables -I INPUT -p tcp --dport 5432 -j ACCEPT
service iptables save
Step 3☆ 初始化數據庫並啓動服務
centos
service postgresql-9.3 initdb
service postgresql-9.3 start
chkconfig postgresql-9.3 on
Step 4☆ 設置PostgreSQL超級用戶密碼
bash
su - postgres
-bash-4.1$ psql
psql (9.3.4)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; --設置密碼爲:postgres
ALTER ROLE
Step 5☆ 建立數據庫並查看數據庫列表
網絡
postgres=# create database test with owner postgres; --建立數據庫 test
CREATE DATABASE
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)
Step 6☆ 建立用戶
tcp
postgres=# create user test with password 'test'; --建立用戶test
CREATE ROLE
Step 7☆ 配置遠程訪問
ide
postgres=# \q
-bash-4.1$ vi /var/lib/pgsql/9.3/data/postgresql.conf
#listen_addresses = ‘localhost’ 改成:listen_addresses = '*' #監聽整個網絡
-bash-4.1$ vi /var/lib/pgsql/9.2/data/pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.3.0/24 trust
Step 8☆ 從新啓動服務並測試
post
service postgresql-9.3 restart
Stopping postgresql-9.3 service: [ OK ]
Starting postgresql-9.3 service: [ OK ]
su - postgres
-bash-4.1$ psql -h 127.0.0.1 -U postgres -W -d test -- W強行要求用戶輸入密碼
Password for user postgres: --輸入postgres用戶密碼
psql (9.3.4)
Type "help" for help.
test=# --進入test數據庫
Step 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)
postgres=# alter database test owner to test; --更改數據全部者爲test用戶
ALTER DATABASE
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 | test | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows)