Source code of this article: GitHub · click here || GitEE · click here
1. Introduction to PostgreSQL
1. Introduction to the database
PostgreSQL is a powerful open source database system, with reliability, stability, data consistency and other characteristics, and can run on all mainstream operating systems, including Linux, Unix, Windows, etc. PostgreSQL is a complete transactional security database that fully supports foreign keys, unions, views, triggers, and stored procedures, and supports most SQL: 2008 standard data types, including integer, numeric, Boolean, and byte Type, character type, date type, time interval type and time type, it also supports the storage of large binary objects, including pictures, sounds and videos. There are native programming interface APIs for many high-level development languages, such as C / C ++, Java, etc., and various documents are also included.
2. Highly open source
The source code of PostgreSQL is freely available, and its license is under a very free open source license, which allows users to use, modify, and distribute the source code of PostgreSQL in various open source or closed source projects. The user can modify or improve the source code according to the user's wishes. Therefore, PostgreSQL is not only a powerful enterprise database system, but also a database development platform where users can develop private, network and commercial software products.
Second, install under Centos7
1. Install RPM
RPM package manager, a packaging and installation tool for Internet download packages, which is included in some Linux distributions.
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2. Install the client
yum install postgresql11
3. Install the server
yum install postgresql11-server
4. Install dependent packages
yum install postgresql11-libs
yum install postgresql11-contrib
yum install postgresql11-devel
5. Initialization and startup
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11
6. Reset password
passwd postgres
7. Login service
su - postgres
psql
8. Install Vim command
yum -y install vim*
9. Configure remote access
# 修改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 ## 修改后需要重启
10. Open port
firewall-cmd --query-port=5432/tcp
firewall-cmd --add-port=5432/tcp
firewall-cmd --add-port=5432/tcp --zone=public --permanent
11. Restart
systemctl restart postgresql-11
Three, create a database
1. Create a user
CREATE USER root01 WITH PASSWORD '123456';
CREATE ROLE;
2. Create a database
CREATE DATABASE db_01 OWNER root01;
CREATE DATABASE;
3. Permission granted
GRANT ALL PRIVILEGES ON DATABASE db_01 TO root01;
GRANT
4. Exit command
\q:退出SQL编辑
exit:退出脚本
Fourth, the basic operation
1. Create a table structure
-- 用户表
CREATE TABLE pq_user (
ID INT NOT NULL,
user_name VARCHAR (32) NOT NULL,
user_age int4 NOT NULL,
create_time TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "pg_user_pkey" PRIMARY KEY ("id")
);
-- 订单表
CREATE TABLE pq_order (
id int not null,
user_id int not null,
order_no varchar (32) not null,
goods varchar (20) not null,
price money not null,
count_num int default 1,
create_time timestamp (6) default current_timestamp,
constraint "pq_order_pkey" primary key ("id")
);
2. Write data
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');
3. Regular query
-- 基础查询
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;
4. Update and delete
-- 更新数据
UPDATE pq_user SET "create_time"='2020-04-09 19:49:57' WHERE ("id"='2');
-- 删除记录
DELETE FROM pq_user WHERE "id" = 2;
Five, source code address
GitHub·地址
https://github.com/cicadasmile/linux-system-base
GitEE·地址
https://gitee.com/cicadasmile/linux-system-base
Recommended reading: environment installation