ubuntu 16.04.1 LTS postgresql安装配置

postgresql安装

--------------------

二进制安装:

wget https://get.enterprisedb.com/postgresql/postgresql-9.5.6-1-linux-x64-binaries.tar.gz

tar xf postgresql-9.5.6-1-linux-x64-binaries.tar.gz -C /usr/local/

useradd postgres

mkdir -p /data/postgresql/{data,log}

chown -R postgres.postgres /data/postgresql/

初始化数据库:

su postgres

cd /usr/local/pgsql/bin

./initdb -E utf8 -D /data/postgresql/data/

启动脚本:

vi /etc/systemd/system/postgresql.service

[Unit]

Description=PostgreSQL database server

After=network.target

[Service]

Type=forking

User=postgres

Group=postgres

OOMScoreAdjust=-1000

Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj

Environment=PG_OOM_ADJUST_VALUE=0

Environment=PGSTARTTIMEOUT=270

Environment=PGDATA=/data/postgresql/data

ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}

ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast

ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s

TimeoutSec=300

[Install]

WantedBy=multi-user.target

启动关闭和开机启动:

systemctl daemon-reload

systemctl enable postgresql

systemctl start postgresql

systemctl stop postgresql

systemctl restart postgresql

postgresql配置

-----------------------

配置文件:默认在data目录下

主配置文件:/data/postgresql/data/postgresql.conf

允许远程访问-> listen_addresses = 'localhost,192.168.30.3'

权限控制文件:/data/postgresql/data/pg_hba.conf

# "local" is for Unix domain socket connections only

local all all md5

# IPv4 local connections:

host all all 192.168.30.2/32 md5

# IPv6 local connections:

host all all ::1/128 md5

重启使配置文件生效:systemctl reload postgresql

注:如果还没有设置postgres用户的密码,需要先把本地设置为trust:

local all all trust

然后设置密码,再修改回来

psql -U postgres

postgres-# ALTER USER postgres PASSWORD 'xxxx';

创建数据库和授权

----------------------

创建用户:postgres=# create user fishing password 'xxxx'

创建数据库:create database fishing owner fishing;

退出数据库,使用新建用户登录新建的数据库:psql -U fishing -d fishing

测试数据库权限:

1. 创建一张表

create table test(

id int primary key not null,

name text not null

);

2. 查看表

fishing-> \dt

List of relations

Schema | Name | Type | Owner

--------+------+-------+---------

public | test | table | fishing

(1 row)

3. 向表中插入数据

insert into test(id,name) values (1,'aa');

select * from test;

4. 删除测试表

drop table test;