创建组和用户(重要提醒:PostgreSQL不能使用root用户来管理)
[root@localhost ~]# mkdir /home/postgresql
[root@localhost ~]# vim /home/postgresql/.bash_profile
export PS1="[\u@\h \W]# "
[root@localhost ~]# groupadd postgresql && useradd -s /bin/bash -d /home/postgresql -g postgresql -M postgresql
[root@localhost ~]# passwd postgresql
[root@localhost ~]# chown -R postgresql:postgresql /home/postgresql
创建相关目录及文件
[root@localhost ~]# mkdir /program/postgresql
[root@localhost ~]# mkdir /program/postgresql/data
[root@localhost ~]# mkdir /program/postgresql/log
[root@localhost ~]# touch /program/postgresql/log/general.log
[root@localhost ~]# touch /program/postgresql/log/server.log
[root@localhost ~]# chown -R postgresql:postgresql /program/postgresql
下载PostgreSQL源码并安装(温馨提示:PostgreSQL的编译安装只需要几分钟,不像MySQL要几个小时)
[root@localhost src]# wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.gz
[root@localhost src]# tar -xf postgresql-16.1.tar.gz
[root@localhost src]# cd postgresql-16.1
[root@localhost postgresql-16.1]# ./configure --prefix=/program/postgresql/
[root@localhost postgresql-16.1]# make
[root@localhost postgresql-16.1]# make install
[root@localhost postgresql-16.1]# chown -R postgresql:postgresql /program/postgresql
PostgreSQL源码已经提供了管理脚本(开机启动、重启等相关操作),将其复制到/etc/init.d目录下并修改里面几个变量即可使用
[root@localhost postgresql-16.1]# cp contrib/start-scripts/linux /etc/init.d/postgresql
[root@localhost postgresql-16.1]# chmod +x /etc/init.d/postgresql && vim /etc/init.d/postgresql
# Installation prefix
#prefix=/usr/local/pgsql
prefix=/program/postgresql
# Data directory
#PGDATA="/usr/local/pgsql/data"
PGDATA="$prefix/data"
# Who to run postgres as, usually "postgres". (NOT "root")
#PGUSER=postgres
PGUSER=postgresql
# Where to keep a log file
#PGLOG="$PGDATA/serverlog"
PGLOG="$prefix/log/server.log"
[root@localhost postgresql-16.1]# chkconfig --add postgresql
[root@localhost postgresql-16.1]# service postgresql start
[root@localhost postgresql-16.1]# service postgresql stop
[root@localhost postgresql-16.1]# service postgresql restart
[root@localhost postgresql-16.1]# service postgresql reload
[root@localhost postgresql-16.1]# service postgresql status
初始化数据库并启动(重要提醒:不能使用root用户执行操作,必须使用postgresql用户执行操作)
[root@localhost ~]# su - postgresql
[postgresql@localhost ~]# /program/postgresql/bin/initdb -D /program/postgresql/data
[postgresql@localhost ~]# cp /program/postgresql/data/postgresql.conf /program/postgresql/data/postgresql.conf.default
[postgresql@localhost ~]# cp /program/postgresql/data/postgresql.conf /program/postgresql/data/postgresql.conf.bak
[postgresql@localhost ~]# cp /program/postgresql/data/pg_hba.conf /program/postgresql/data/pg_hba.conf.default
[postgresql@localhost ~]# cp /program/postgresql/data/pg_hba.conf /program/postgresql/data/pg_hba.conf.bak
[postgresql@localhost ~]# chown -R postgresql:postgresql /program/postgresql
[postgresql@localhost ~]# /program/postgresql/bin/pg_ctl -D /program/postgresql/data -l /program/postgresql/log/general.log -m smart status
pg_ctl: no server running
[postgresql@localhost ~]# /program/postgresql/bin/pg_ctl -D /program/postgresql/data -l /program/postgresql/log/general.log -m smart start
waiting for server to start.... done
server started
[postgresql@localhost ~]# /program/postgresql/bin/pg_ctl -D /program/postgresql/data -l /program/postgresql/log/general.log -m smart status
pg_ctl: server is running (PID: 19084)
/program/postgresql/bin/postgres "-D" "/program/postgresql/data"
[postgresql@localhost ~]# /program/postgresql/bin/pg_ctl -D /program/postgresql/data -l /program/postgresql/log/general.log -m smart stop
waiting for server to shut down.... done
server stopped
[postgresql@localhost ~]#
说明①:和MySQL类似,PostgreSQL也可以把data目录清空,然后重新初始化。
说明②:常用管理命令包括:status、start、stop、reload、restart、……
设置允许远程连接(仅限开发环境,生产环境务必禁止远程连接)
[postgresql@localhost ~]# vim /program/postgresql/data/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
listen_addresses = '*'
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#reserved_connections = 0 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/tmp' # comma-separated list of directories
# (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# (change requires restart)
# - TCP settings -
# see "man tcp" for details
[postgresql@localhost ~]# vim /program/postgresql/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# 允许远程连接(METHOD列必须设为md5,否则可以免密码连接):
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
[postgresql@localhost ~]# /program/postgresql/bin/pg_ctl -D /program/postgresql/data -l /program/postgresql/log/general.log -m smart restart
列出当前存在的数据库
[postgresql@localhost ~]# /program/postgresql/bin/psql -l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
-----------+------------+----------+-----------------+-------------+-------------+------------+-----------+---------------------------
postgres | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | |
template0 | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | | =c/postgresql +
| | | | | | | | postgresql=CTc/postgresql
template1 | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | | =c/postgresql +
| | | | | | | | postgresql=CTc/postgresql
(3 rows)
[postgresql@localhost ~]#
说明①:Name列是数据库名,Owner列是数据库拥有者的用户名。
说明②:postgresql是超级用户,类似MySQL的root用户。
修改超级用户(postgresql)的密码
[postgresql@localhost ~]# /program/postgresql/bin/psql postgres # 进入PostgreSQL命令行,并切换到postgres数据库
psql (16.1)
Type "help" for help.
postgres=# ALTER USER postgresql WITH PASSWORD '新密码(明文)';
ALTER ROLE
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
-----------+------------+----------+-----------------+-------------+-------------+------------+-----------+---------------------------
postgres | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | |
template0 | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | | =c/postgresql +
| | | | | | | | postgresql=CTc/postgresql
template1 | postgresql | UTF8 | libc | zh_CN.UTF-8 | zh_CN.UTF-8 | | | =c/postgresql +
| | | | | | | | postgresql=CTc/postgresql
(3 rows)
postgres=# \du
List of roles
Role name | Attributes
------------+------------------------------------------------------------
postgresql | Superuser, Create role, Create DB, Replication, Bypass RLS
postgres=# \q
[postgresql@localhost ~]#
说明①:这时已经可以使用postgresql用户远程连接postgres数据库了。
说明②:和MySQL不同,远程客户端(如Navicat等)在连接PostgreSQL服务器时就需要指定数据库,而不是先连上服务器再选择数据库。
下面是常用SQL语句使用示例:
-- 清屏(注意:叹号和clear之间有空格)
-- \! clear
-- 列出所有数据库
-- \l
-- 切换数据库
-- \c 数据库名
-- 列出所有用户
-- \du
-- 创建testuser用户
CREATE ROLE testuser WITH PASSWORD '密码(明文)';
-- 说明①:创建用户可以用ROLE,也可以用USER,区别在于ROLE默认没有登录权限(远程登录),而USER默认有登录权限。
-- 说明②:用户的登录权限可以随时授予或撤销,所以用哪个都无所谓。
-- 授予testuser用户登录权限
ALTER ROLE testuser LOGIN;
-- 撤销testuser用户登录权限
ALTER ROLE testuser NOLOGIN;
-- 删除testuser用户
DROP ROLE testuser;
-- 创建testdb数据库
CREATE DATABASE testdb;
-- 说明:创建数据库会默认自带一个名为public的模式。
-- 删除当前数据库的public模式,注意是当前数据库,如果要删除别的数据库的模式只能先切换
DROP SCHEMA public CASCADE;
-- 说明:CASCADE是一个选项,表示与该模式相关联的所有对象(表、视图、索引等)也会一并删除。
-- 删除testdb数据库
DROP DATABASE testdb;
-- 把testdb数据库的拥有者改为testuser用户
ALTER DATABASE testdb OWNER TO testuser;
-- 创建一个名为private的模式(SCHEMA)
CREATE SCHEMA "private" AUTHORIZATION "testuser";
-- 删除prefix_user表(如果该表存在的话)
DROP TABLE IF EXISTS "private"."prefix_user";
-- 创建prefix_user表
CREATE TABLE "private"."prefix_user"
(
"uid" INT4 NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 ),
"name" VARCHAR(255) COLLATE "pg_catalog"."default" DEFAULT '' :: CHARACTER VARYING,
CONSTRAINT "prefix_user_pkey" PRIMARY KEY ("uid")
);
-- 设置prefix_user表的拥有者为testuser
ALTER TABLE "private"."prefix_user" OWNER TO "testuser";
-- uid字段的注释
COMMENT ON COLUMN "private"."prefix_user"."uid" IS '用户UID';
-- name字段的注释
COMMENT ON COLUMN "private"."prefix_user"."name" IS '用户姓名';
-- prefix_user表的注释
COMMENT ON TABLE "private"."prefix_user" IS '用户表';
-- 插入记录(一次多条)
INSERT INTO "private"."prefix_user" ("name")
VALUES ('刘一'), ('陈二'), ('张三'), ('李四'), ('王五'), ('赵六'), ('孙七'), ('周八'), ('吴九'), ('郑十');
------------------------- 总结 -------------------------
-- 1、PostgreSQL的模式(SCHEMA)类似编程语言的命名空间,也可以简单粗暴地将其理解为数据表的目录,它主要是用来组织和管理数据表。
-- 举个例子说明,系统的商城模块需要创建商品表、订单表等相关数据表,这样就可以先创建一个名为shop的模式,然后所有跟商城有关的
-- 数据表都创建在shop模式下,这样系统的每个模块的数据表都有各自的模式,结构就会更加清晰和方便管理。
-- 2、由于模式(SCHEMA)的存在,所以同一个数据库里可以有不同模式但同名的数据表,这和编程语言由于命名空间的存在,所以同一个项目里
-- 可以有不同命名空间但同名的类是一样的。
-- 3、要删除一个模式(SCHEMA),必须先将该模式下的所有对象(表、视图、索引等)删除。