(https://www.postgresql.org/download/)
软件:postgresql-9.6.6-3-linux-x64.runroot@UbuntuPC:/home/belong/download# cp postgresql-9.6.6-3-linux-x64.run /opt/softwarepackages/
root@UbuntuPC:/home/belong/download# cd /
root@UbuntuPC:/# cd /opt/softwarepackages/
root@UbuntuPC:/opt/softwarepackages# chmod 777 postgresql-9.6.6-3-linux-x64.run
root@UbuntuPC:/opt/softwarepackages# ./postgresql-9.6.6-3-linux-x64.run
开始安装......:备注:
root@UbuntuPC:/opt# sudo passwd postgres
推荐密码和用户密码一致 1qaz!QAZ
root@UbuntuPC:/opt# su - postgres
$ pwd
/opt/PostgreSQL/9.6
$ exit
(3)复制环境变量文件
root@UbuntuPC:/opt# cp /.bash_profile /opt/PostgreSQL/9.6
root@UbuntuPC:/opt# cp /.bashrc /opt/PostgreSQL/9.6
(4)修改安装目录拥有者/opt/PostgreSQL/
root@UbuntuPC:/opt# chown -R postgres:postgres /opt/PostgreSQL/
(5)修改环境变量
使用
root@UbuntuPC:/opt# gedit /.bashrc
打开,文件结尾追加
#postgresqlforzw add 2017-12-19
export PGHOME=/opt/PostgreSQL/9.6
export PATH=$PGHOME/bin:$PATH
export PGDATA=$PGHOME/data
export LD_LIBRARY_PATH=$PGHOME/lib
(6)初始化数据库目录
root@UbuntuPC:/opt# su postgresql
$ bin/initdb -D data
(7)通过postmaster脚本启动
$ bin/postmaster -D /opt/PostgreSQL/9.6/data/
2017-12-19 22:28:48 CST FATAL: lock file "postmaster.pid" already exists
2017-12-19 22:28:48 CST HINT: Is another postmaster (PID 981) running in data directory "/opt/PostgreSQL/9.6/data"?
$
(8)其他用户访问
$ vi data/postgresql.conf
修改:
listen_addresses = '*'
port = 5432
4-1、登录数据库
4-2、查看数据库相关信息
4-3、创建表空间位置文件
一般存放在/srv目录下
postgres=# \q
$ exit
root@UbuntuPC:~# cd /srv/
root@UbuntuPC:/srv# mkdir -p databases/postgresql
root@UbuntuPC:/srv# su - postgres
$ cd /srv/databases/postgresql
$ mkdir data1 data2 data3
4-4,创建表空间
$ ./scripts/runpsql.sh
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql.bin (9.6.6)
Type "help" for help.
Cannot read termcap database;
using dumb terminal settings.
postgres=#
postgres=# create tablespace tb_hcie location '/srv/databases/postgresql/data1';
4-5,创建数据库
postgres=# create database db_hcie with tablespace = tb_hcie;
4-6,进入数据库
postgres=# \q
$ ./scripts/runpsql.sh
Server [localhost]:
Database [postgres]: db_hcie
Port [5432]:
Username [postgres]:
Password for user postgres:
psql.bin (9.6.6)
Type "help" for help.
Cannot read termcap database;
using dumb terminal settings.
db_hcie=#
4-7,特定数据库创建用户(非角色)
db_hcie=# create user u_hcie password '123456';
CREATE ROLE
db_hcie=#
db_hcie =# grant create on tablespace tb_hice to u_hcie;
4-8,创建模式
db_hcie=#
db_hcie=# create schema s_0_temp;
CREATE SCHEMA
db_hcie=# create schema s_1_basics;
CREATE SCHEMA
db_hcie=# create schema s_2_count,s_3_middle;
CREATE SCHEMA
4-9,模式所有权利赋予(最高管理员)
db_hcie=# grant all privileges on schema s_0_temp,s_1_basics,s_2_count,s_3_middle to postgres;
GRANT
4-10、模式使用权限赋予用户
db_hcie=# grant usage on schema s_0_temp,s_1_basics,s_2_count,s_3_middle to u_hcie;
GRANT
db_hcie=# grant select,insert,update,delete on all tables in schema s_0_temp,s_1_basics,s_2_count,s_3_middle to u_hcie;
GRANT
db_hcie=#
db_hcie=# \q
$
4-11、进入测试数据库
$ ./scripts/runpsql.sh
Server [localhost]:
Database [postgres]: db_hcie
Port [5432]:
Username [postgres]: u_hcie
Password for user u_hcie:
psql.bin (9.6.6)
Type "help" for help.
Cannot read termcap database;
using dumb terminal settings.
db_hcie=>