Step 1☆ 安裝PostgreSQL
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☆ 初始化数据库并启动服务
service postgresql-9.3 initdb service postgresql-9.3 start chkconfig postgresql-9.3 on
Step 4☆ 设置PostgreSQL超级用户密码
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☆ 创建用户
postgres=# create user test with password 'test'; --创建用户test CREATE ROLE
Step 7☆ 配置远程访问
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☆ 重新启动服务并测试
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)