postgresql安裝,初始化配置,以及新建用戶

安裝

官網 ,根據你的系統選好版本,然後一步步走下來即可。比如我是centos7,安裝pgsql10:

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm

yum install postgresql10

yum install postgresql10-server
#初始化
/usr/pgsql-10/bin/postgresql-10-setup initdb
#開機啓動
systemctl enable postgresql-10
#啓動
systemctl start postgresql-10

pgsql10配置文件位置默認在:/var/lib/pgsql/10/data
首次密碼登錄需要修改pg_hba.conf,把裏面的ident全部換成md5


否則會有如下錯誤:

psql: FATAL: Ident authentication failed for user

設置允許訪問

  • 首先允許內網其它網段訪問,或者全網: 0.0.0.0/0
vi /etc/postgresql/9.5/main/pg_hba.conf
#增加下面一行
# TYPE TABASE        USER            ADDRESS                 METHOD
host    all             all             192.168.1.1/24          md5
  • 設置監聽所有ip
vi /etc/postgresql/9.5/main/postgresql.conf
#修改listen_address
listen_address = '*'
  • 完成後重啓服務器
service postgersql restart

創建用戶

  • 切換到postgres超級管理員
su postgres
#創建一個初始數據庫
createdb tempdb
#創建超級用戶pgdbo
createuser -s -P pgdbo
  • 然後就可以用該用戶登錄了
psql -U pgdbo -h localhost tempdb
#登錄後修改密碼
alter user postgres with password 'u8soft'

附:常用控制檯命令

命令 作用
\h 查看所有sql命令,\h select 等可以查看具體命令
? 查看所有psql命令
\d 查看當前數據庫所有表
\d [tablename] 查看具體的表結構
\du 查看所有用戶
\l 查看所有數據庫
\e 打開文本編輯器

SQL控制檯操作

--創建數據庫
create database test;
--刪除數據庫
drop database test;
--重命名數據庫(該數據庫必須沒有活動的連接)
alter database test1 rename to test;
--以其他數據庫爲模板創建數據庫(表結構、數據都會複製)
create database test1 template test;

--將查詢結果寫入文件
\o /home/developer/test.txt
select * from test;
--列狀顯示
\w
--再一次\o關閉寫入,否則是連續寫入的
\o

linux命令行操作

#備份數據庫test,test.bak裏面都是sql命令
pg_dump -U pgdbo -h localhost test > test.bak
#恢復備份到數據庫test(test須事先創建好)
psql -U pgdbo -h localhost test < test.bak
參考

http://pylixm.cc/posts/2017-11-05-postgresql-install.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章