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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM