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/linux

初始化數據庫:
su postgres
cd /usr/local/pgsql/bin
./initdb -E utf8 -D /data/postgresql/data/sql

啓動腳本:
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=300dom

[Install]
WantedBy=multi-user.targetsocket

啓動關閉和開機啓動:
systemctl daemon-reload
systemctl enable postgresql
systemctl start postgresql
systemctl stop postgresql
systemctl restart postgresqlpost


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 md5rest

重啓使配置文件生效:systemctl reload postgresqlpostgresql

注:若是尚未設置postgres用戶的密碼,須要先把本地設置爲trust:
local all all trust
而後設置密碼,再修改回來
psql -U postgres
postgres-# ALTER USER postgres PASSWORD 'xxxx';server


建立數據庫和受權
----------------------
建立用戶: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;

相關文章
相關標籤/搜索