一、PostgreSQL简介

PostgreSQL数据库是目前功能强大的开源数据库,支持丰富的数据类型(如JSON和JSONB类型、数组类型)和自定义类型。而且他提供了丰富的接口,可以很容易的扩展它的功能,如可以再GiST框架下实现自己的索引类型等。PostgreSQL是完全的事务安全性数据库,完整地支持外键、联合、视图、触发器和存储过程(并支持多种语言开发存储过程)。它支持了大多数的SQL:2008标准的数据类型,包括整型、数值值、布尔型、字节型、字符型、日期型、时间间隔型和时间型,它也支持存储二进制的大对像,包括图片、声音和视频。PostgreSQL对很多高级开发语言有原生的编程接口,如C/C++、Java、.Net、Perl、Python、Ruby、Tcl 和ODBC以及其他语言等,也包含各种文档。

从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编写,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。

PostgreSQL强壮的一个原因源于它的架构。和商业数据库一样,PostgreSQL可以用于C/S(客户/服务器)环境。这对于用户和开发人员有很多好处。

二、Linux 上安装 PostgreSQL

linux下安装PostgreSQL可采用三种方式,二进制已编绎安装包、yum安装、源码安装三种方式进行安装

1.二进制已编绎安装包方式安装

第一种安装方式:通过二进制已编绎安装包安装

(1)下载二进制包

https://www.enterprisedb.com/download-postgresql-binaries
https://get.enterprisedb.com/postgresql/postgresql-10.22-1-linux-x64-binaries.tar.gz

(2)创建postgres用户

#创建用户
useradd postgres
#设置密码
passwd postgres

(3)解压

tar -xvf postgresql-10.12-1-linux-x64-binaries.tar.gz -C /home/postgres/

(4)创建data目录

mkdir -p /home/postgres/pgsql/data
mkdir -p /home/postgres/pgsql/logs

(5)初始化

./bin/initdb -E utf8 -D /home/postgres/pgsql/data

初始化结果如下:

[postgres@localhost pgsql]$ ./bin/initdb -E utf8 -D /home/postgres/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /home/postgres/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... PRC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    ./bin/pg_ctl -D /home/postgres/pgsql/data -l logfile start

(6)启动

./bin/pg_ctl -D /home/postgres/pgsql/data -l /home/postgres/pgsql/logs/pgsql.log start

(7)关闭

./bin/pg_ctl -D /home/postgres/pgsql/data stop

(8)登录postgresql数据库

./psql

(9)创建用户和数据库并授权

create user test_user with password '123456'; // 创建用户
create database test_db owner test_user; // 创建数据库
grant all privileges on database test_db to test_user; // 授权

(10)退出psql(输入 \q 再按回车键即可)

\q

(11)连接数据库

./bin/psql -h 127.0.0.1 -d test_db -U test_user -p 5432

(12)开启远程访问

####修改postgresql.conf文件,取消 listen_addresses 的注释,将参数值改为“*”

####修改pg_hba.conf文件,增加下图红框部分内容
host    all             all             0.0.0.0/0            md5

####navicat
https://www.cnblogs.com/zhi-leaf/p/11432054.html

2.yum安装PostgreSQL

第二种安装方式:通过yum安装

(1)安装过程官网参考

https://www.postgresql.org/download/linux/redhat/

(2)安装过程步骤

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql10-server

## 采用ln -s方式挂载/var/lib/pgsql/10/data目录用来修改数据目录,/home/postgresdata为例
mkdir /home/postgresdata -p
chown -R postgres:postgres /home/postgresdata
rm -rf /var/lib/pgsql/10/data
ln -s /home/postgresdata /var/lib/pgsql/10/data
chown -R postgres:postgres /var/lib/pgsql/10/data

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
sudo systemctl enable postgresql-10
sudo systemctl start postgresql-10

3.源码方式安装PostgreSQL

第三种安装方式:通过源码安装
部分内容参考了此文https://www.cnblogs.com/wsum/p/15522211.html

(1)下载源码

##下载地址和源码
https://www.postgresql.org/download/
postgresql-14.5.tar.gz

(2)创建postgres用户

#创建用户
useradd postgres
#设置密码
passwd postgres

(3)进行源码安装

以安装到/opt/postgresql目录下为例

##1.解压
tar -xvf postgresql-14.5.tar.gz -C /opt/

##2.yum依赖
yum install -y gcc gcc-c++
yum install -y readline-devel
yum install -y zlib-devel

##3.编绎,并安装到/opt/postgresql目录
mkdir /opt/postgresql
cd /opt/postgresql-14.5
./configure --prefix=/opt/postgresql
make
make install

#4.准备数据目录
mkdir -p /opt/postgresql/pgsqldata
chown -R postgres:postgres /opt/postgresql/pgsqldata

#5.切换到postgres用户
su postgres
/opt/postgresql/bin/initdb -D /opt/postgresql/pgsqldata #初始化数据库
mkdir /opt/postgresql/pgsqldata/logs
/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgsqldata -l /opt/postgresql/pgsqldata/logs/pgsql.log start #启动
/opt/postgresql/bin/createdb test #创建测试库
/opt/postgresql/bin/psql test #进入数据库

#6.修改管理员密码
ALTER USER postgres WITH PASSWORD '123456';

(4)设置postgresql 服务自启动

如果需要随开机启机,可以制作成自启动服务如下:

######postgresql 服务
cat > /usr/lib/systemd/system/postgresql.service << EOF
[Unit]
Description=postgreSQL Server
After=network.target

[Service]
User=postgres
Group=postgres
Type=forking
TimeoutSec=0
PermissionsStartOnly=true
PIDFile=/opt/postgresql/pgsqldata/postmaster.pid
ExecStart=/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgsqldata -l /opt/postgresql/pgsqldata/logs/pgsql.log start
ExecReload=/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgsqldata reload
ExecStop=/opt/postgresql/bin/pg_ctl -D /opt/postgresql/pgsqldata stop
PrivateTmp=true

LimitNOFILE = 65535
Restart=on-failure
RestartSec=3
RestartPreventExitStatus=1
PrivateTmp=false


[Install]
WantedBy=multi-user.target
EOF

######服务启动停止
systemctl daemon-reload
systemctl stop postgresql
systemctl start postgresql
systemctl enable postgresql

三、基本操作

1.查看当前的数据库列表

####查看当前的数据库列表
\l 

2.建表

####建表
create table test(
   id bigint ,
   name varchar(50),
   age int,
   password varchar(30)
 );

3.插入与查询

####插入
insert into test values(1,'test',21,'123');

####查询
select * from test;

4.查看pgsql版本

SELECT version();

5.查看用户名和密码

SELECT * FROM pg_authid;

6.获取服务器上所有数据库信息

SELECT * FROM pg_database ORDER BY datname;

7.得到db中所有表的信息

#获取所有库表信息
select * from pg_tables ORDER BY schemaname;

#获取当前
\d 库名 获取库中数据表名列表
\d 表名 获取表结构字段描述

8.备份与还原

/opt/postgresql/bin/pg_dump -h localhost -U postgres -p 5432 -d test -s -f /root/data.sql
/opt/postgresql/bin/psql -h localhost -p 5432 -U postgres -W -d test < /root/data.sql

9.更多参考PostgreSQL 教程

PostgreSQL 教程:
https://www.runoob.com/postgresql/postgresql-tutorial.html

其他

psql: FATAL: Ident authentication failed for user

#
vi /var/lib/pgsql/10/data/pg_hba.conf 

将localhost和127.0.0.1的两行的ident修改成trust,然后重启服务

在这里插入图片描述

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐