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)