[PostgreSQL] CentOS 7, one of the series, installs PGSQL15 version (1)

Table of contents

1. What is PostgreSQL?

2. PostgreSQL installation

2.1 Installation dependencies

2.2 Executing the installation

2.3 Database initialization

2.4 Configure environment variables

2.5 Create a database

2.6 Configure remote

2.7 Test remote

3. Commonly used commands

Fourth, user creation and database permissions


1. What is PostgreSQL?


PostgreSQL is an object-relational database management system (ORDBMS) based on POSTGRES, version 4.2 developed by the Department of Computer Science, University of California, Berkeley . Many of the concepts that POSTGRES pioneered appeared much later in some commercial database systems.

PostgreSQL is the open source successor to the original Berkeley code. It supports most of the SQL standard and provides many features:

  • complex query
  • foreign key
  • trigger
  • updatable view
  • transactional integrity
  • Multi-version concurrency control

Also, PostgreSQL can be extended in many ways, e.g. by adding new:

  • type of data
  • function
  • operator
  • aggregate function
  • index method
  • procedural language

Official website address: https://www.postgresql.org/download/linux/redhat/

Connection tool: https://www.pgadmin.org/download/


2. PostgreSQL installation


2.1 Installation dependencies


# 获取所需依赖包
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libzstd-1.5.5-1.el7.x86_64.rpm
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/llvm5.0-devel-5.0.1-7.el7.x86_64.rpm
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/llvm5.0-5.0.1-7.el7.x86_64.rpm
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/llvm5.0-libs-5.0.1-7.el7.x86_64.rpm
 
 
yum install -y libzstd-1.5.5-1.el7.x86_64.rpm
yum install -y centos-release-scl-rh llvm5*
yum install -y epel-release

Dependency download and installation complete

2.2 Executing the installation


wget --no-check-certificate  https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# 安装 PostgreSQL
sudo yum install -y postgresql15-server postgresql15-devel

The installation is complete

2.3 Database initialization


# 初始化DB
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb

# 开机启动|启动|重启|状态|停止 命令
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
sudo systemctl restart postgresql-15
sudo systemctl status postgresql-15
sudo systemctl stop postgresql-15

confirm start

2.4 Configure environment variables


Add the following configuration to the /etc/profile file:

# PGSQL_HOME
export PGSQL_HOME=/usr/pgsql-15
export PATH=$PATH:$PGSQL_HOME/bin

After the configuration is complete, remember to source it.

2.5 Create a database


# 切换用户
su postgres
# 进入命令行
psql

#创建DB
create database test_db
    
alter user postgres with password 'winner@001';

Enter the database command line to create the database and

Start default port: 5432

2.6 Configure remote


# 编辑
vim /var/lib/pgsql/15/data/postgresql.conf
修改参数:
listen_addresses = '*'

# 编辑配置
vim /var/lib/pgsql/15/data/pg_hba.conf
# 添加内容
host    all             all              0.0.0.0/0              md5

# 重启
systemctl restart postgresql-15

2.7 Test remote


command line test

psql -h 主机IP -p 端口  -U 用户名 -W -d 数据库

Use Navicat162 to connect and download the latest Navicat, otherwise an error will be reported


3. Commonly used commands


Enter the command su postgres and then enter the command psql If everything is normal, the system prompt will change to "postgres=#", indicating that you have entered the database console at this time

  •  \l: list the databases in the system 
  • \h: View the explanation of SQL commands, such as \h select.
  • \?: View the list of psql commands.
  • \l: List all databases.
  • \c [database_name]: connect to other databases.
  • \d: List all tables in the current database.
  • \du: List all users.
  • \e: Open a text editor.
  • \conninfo: List current database and connection information.

Switch database and query users table


Fourth, user creation and database permissions


Add a user named kangll, and set the password '123456', for the kangll user, create a database called kangll_test

postgres=# create user kangll with password '123456';
CREATE ROLE
postgres=# create database kangll_test owner kangll;
CREATE DATABASE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 kangll    |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \l

Grant all permissions of the kangll_test library to the kangll user

grant all privileges on database kangll_test to kangll;
 
grant usage on schema public to kangll;
 
grant all privileges on all tables in schema public to kangll;
 
grant all privileges on all sequences in schema public to kangll;
 
grant select,insert,update,delete on all tables in schema public to kangll;
 
grant all on schema public to kangll;

pg By default, all users can create tables in the public schema, and read-only users are not allowed to create tables.

Create stu table in kang_test library

CREATE TABLE stu(
  stu_id BIGINT NOT NULL,
  stu_name VARCHAR(255) NOT NULL);

# 插入数据
INSERT INTO stu VALUES(1, 'kangll');

# 查询
SELECT * FROM stu;

Change the role to a superuser role

alter role kangll  with superuser;

revoke user privileges

/* 撤销用户权限 */
REVOKE privileges ON tablename FROM user;

Guess you like

Origin blog.csdn.net/qq_35995514/article/details/131998381