一、PostgreSQL簡介
1、數據庫簡介
PostgreSQL是一個功能強大的開源數據庫系統,具有可靠性、穩定性、數據一致性等特點,且可以運行在所有主流操作系統上,包括Linux、Unix、Windows等。PostgreSQL是完全的事務安全性數據庫,完整地支持外鍵、聯合、視圖、觸發器和存儲過程,支持了大多數的SQL:2008標準的數據類型,包括整型、數值型、布爾型、字節型、字符型、日期型、時間間隔型和時間型,它也支持存儲二進制的大對像,包括圖片、聲音和視頻。對很多高級開發語言有原生的編程接口API,如C/C++、Java、等,也包含各種文檔。
2、高度開源
PostgreSQL的源代碼可以自由獲取,它的授權是在非常自由的開源授權下,這種授權允許用戶在各種開源或是閉源項目中使用、修改和發佈PostgreSQL的源代碼。用戶對源代碼的可以按用戶意願進行任何修改、改進。因此,PostgreSQL不僅是一個強大的企業級數據庫系統,也是一個用戶可以開發私用、網絡和商業軟件產品的數據庫開發平臺。
二、Centos7下安裝
1、安裝RPM
RPM軟件包管理器,一種用於互聯網下載包的打包及安裝工具,它包含在部分Linux分發版中。
<code>yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm/<code>
2、安裝客戶端
<code>yum install postgresql11/<code>
3、安裝服務器端
<code>yum install postgresql11-server/<code>
4、安裝依賴包
<code>yum install postgresql11-libs
yum install postgresql11-contrib
yum install postgresql11-devel/<code>
5、初始化和啟動
<code>/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11/<code>
6、重置密碼
<code>passwd postgres/<code>
7、登錄服務
<code>su - postgres
psql/<code>
8、安裝Vim命令
<code>yum -y install vim*/<code>
9、配置遠程訪問
<code># 修改01
vim /var/lib/pgsql/11/data/postgresql.conf
listen_addresses = 'localhost'
修改為
listen_addresses = '*'
# 修改02
vim /var/lib/pgsql/11/data/pg_hba.conf
添加內容
host all all 0.0.0.0/0 trust ## 修改後需要重啟/<code>
10、開放端口
<code>firewall-cmd --query-port=5432/tcp
firewall-cmd --add-port=5432/tcp
firewall-cmd --add-port=5432/tcp --zone=public --permanent/<code>
11、重新啟動
<code>systemctl restart postgresql-11/<code>
三、創建數據庫
1、創建用戶
<code>CREATE USER root01 WITH PASSWORD '123456';
CREATE ROLE;/<code>
2、創建數據庫
<code>CREATE DATABASE db_01 OWNER root01;
CREATE DATABASE;/<code>
3、權限授予
<code>GRANT ALL PRIVILEGES ON DATABASE db_01 TO root01;
GRANT/<code>
4、退出命令
<code>\\q:退出SQL編輯
exit:退出腳本/<code>
四、基本操作
1、創建表結構
<code>-- 用戶表
CREATE TABLE pq_user (
\tID INT NOT NULL,
\tuser_name VARCHAR (32) NOT NULL,
\tuser_age int4 NOT NULL,
\tcreate_time TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP,
\tCONSTRAINT "pg_user_pkey" PRIMARY KEY ("id")
);
-- 訂單表
CREATE TABLE pq_order (
\tid int not null,
\tuser_id int not null,
\torder_no varchar (32) not null,
\tgoods varchar (20) not null,
\tprice money not null,
\tcount_num int default 1,
\tcreate_time timestamp (6) default current_timestamp,
\tconstraint "pq_order_pkey" primary key ("id")
);/<code>
2、寫入數據
<code>INSERT INTO pq_user ("id", "user_name", "user_age", "create_time")
VALUES ('1', 'user01', '18', '2020-04-09 19:44:57.16154');
INSERT INTO pq_order ("id", "user_id", "order_no", "goods", "price", "count_num", "create_time")
VALUES ('1', '1', 'NO20200329652362', '書籍', '$12.20', '3', '2020-04-09 20:01:09.660208');/<code>
3、常規查詢
<code>-- 基礎查詢
select * from pq_user t1 where t1.id='2' and t1.user_name='user01';
select * from pq_user t1 where t1.id !='2' order by create_time desc;
-- 連接查詢
select * from pq_user t1 join pq_order t2 on t1.id=t2.user_id;
select * from pq_user t1 left join pq_order t2 on t1.id=t2.user_id;/<code>
4、更新和刪除
<code>-- 更新數據
UPDATE pq_user SET "create_time"='2020-04-09 19:49:57' WHERE ("id"='2');
-- 刪除記錄
DELETE FROM pq_user WHERE "id" = 2;/<code>
推薦相關閱讀:
《
》《 》
《 》
《 》
閱讀更多 知了一笑 的文章