Install postgreSQL under Centos

Recently, Beijing yq is serious, and I am learning postgreSQL at home. This time, I used the Centos environment to install it, and recorded it for future reference.

The first step, download and install

Postgre supports both windows and linux systems, and this time it is installed in the linux environment.
Download address: postgreSQL official website
Select the Linux system on the official website, use yum to download the software, just select the corresponding version and platform, and the download and installation script can be generated.
insert image description here
The build script is as follows.

// 下载
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
// 安装
yum install -y postgresql15-server
// 初始化数据库
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
// 设置开机启动
sudo systemctl enable postgresql-15

Follow the above steps to install. After the installation is successful, it will createDefault account postgresdefault database postgres, to store the basic information of the database, which will be initializedsuperuser postgres
insert image description here

The second step, configure the database information
1) Change password
// 切换到postgres用户
su postgres
// 切换SQL模式
psql
// 修改密码
alter user postgres with password 'postgres123';
// 创建test用户
create user test with password 'test';
// 授权
grant all privileges on database mydb to test;
// 退出
\q

In order to connect through tools such as Navicat, the following settings can be made

2) Configure remote access

Add port 5432 to firewall list

// 打开防火墙端口
sudo firewall-cmd --add-port=5432/tcp --permanent
// 刷新防火墙
sudo firewall-cmd --reload
3) Modify the configuration file

Modify the listening address to any address, that is, modifypostgresql.confdocument.

// 打开配置文件
vi /var/lib/pgsql/15/data/postgresql.conf
// 打开监听注释,监听地址改为*
listen_addresses = '*'
// 保存退出 esc
:wq

Modify the configuration file as shown in the figure below.
insert image description here

Allow all IP access, that is, modifypg_hba.confdocument

// 打开配置文件
vi /var/lib/pgsql/15/data/pg_hba.conf
// 新增一行,若连接不上则将 scram-sha-256修改为trust 信任模式
host  all  all 0.0.0.0/0 scram-sha-256
// 重启服务
sudo systemctl restart postgresql-15

The configuration file is added as shown in the figure below.
insert image description here
Use Navicat to connect to the database, and the connection information is shown in the figure below.
insert image description here

The third step, common operation commands

Database , i.e. create database, switch database, etc.

// 切换到postgres用户
su postgres
// 创建数据库 mydb
create database mydb;
// 查看所有数据库
\l
// 切换到mydb数据库
\c mydb
// 删除
drop database mydb;

The result of the operation is shown in the figure below.
insert image description here
Datatables , i.e. creating tables, inserting data, etc.

// 创建表
create table business_order(id serial primary key,order_no varchar(255),sku_name varchar(255));
// 插入数据
insert into business_order(order_no,sku_name)values('20221126000001','iphone14 plus');
// 查看表内容
select * from business_order;
// 查看表结构
\d business_order
// 查看所有表,序列等
\d

The create table statement is similar to other database languages, note that the primary key increment sequence is usedserial, mysql is usingauto_increment.
insert image description here
View table structure
insert image description here

The fourth step, database backup and restore

In actual work, the database will be backed up and restored. There are three main formats for backup
.bakthe compressed binary
.sqli.e. plaintext storage
.tarThat is, the tarball compression format
database backup is divided into single database backup , usepg_dumpcommand; for all database backups , usepg_dumpallOrder.

// 切换到postgres用户
su postgres
// 单数据库备份-导出到当前目录
pg_dump mydb > mydb.bak
// 所有数据库备份,
pg_dumpall > backup.bak
// 整个数据库备份到指定位置
pg_dump -f /tmp/mydb.bak mydb
// 备份postgres数据库中business_order表
pg_dump -U postgres -f /tmp/mydb.sql -t business_order postgres
// 数据库恢复-直接恢复,注意先新增空数据库
psql -f /tmp/mydb.bak mydb
// 数据库恢复
pg_restore -U postgres -d business_order /temp/mydb.bak
// 查看目录
ls -l

The execution result is shown in the figure below.
insert image description here

exception handling

1) Navicat connection error
error code: authentication method 10 not supported
Solution: In the pg_hba.conf file, the newly added mode of 0.0.0.0/0 can be changed totrust

Guess you like

Origin blog.csdn.net/u012190388/article/details/128025382