postgres安装使用
yum安装
https://www.postgresql.org/download/linux/redhat/
# 源码包 生产建议使用
https://www.postgresql.org/ftp/source/
[root@vm ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 下载postgres的yum包
[root@node1 ~]# yum -y install yum-utils
[root@node1 ~]# yumdownloader --destdir=/tmp --resolve postgresql12-server
[root@node1 ~]# ls /tmp/
libicu-50.2-4.el7_7.x86_64.rpm postgresql12-libs-12.10-1PGDG.rhel7.x86_64.rpm
postgresql12-12.10-1PGDG.rhel7.x86_64.rpm postgresql12-server-12.10-1PGDG.rhel7.x86_64.rpm
安装postgres
[root@vm ~]# yum install -y postgresql14-server
# 初始化
[root@vm ~]# /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK
#开机自启动
[root@vm ~]# systemctl enable postgresql-14 --now
PostgreSQL安装成功之后,会默认创建一个名为postgres的Linux用户,
初始化数据库后,会有名为postgres的数据库,来存储数据库的基础信息,例如用户信息等等,相当于MySQL中默认的名为mysql数据库。
postgres数据库中荟初始化一名超级用户postgres
为了方便我们使用postgres账号进行管理,我们可以修改该账号的密码
[root@vm ~]# grep postgres /etc/passwd
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
su postgres
ALTER USER postgres WITH PASSWORD NewPassword’;
[root@vm ~]# vim /var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*'
[root@vm ~]# vim /var/lib/pgsql/14/data/pg_hba.conf
# IPv4 local connections: # 新增一行 peer 不需要输密码
host all all 0.0.0.0/0 scram-sha-256
#内容解析:
#TYPE(主机类型)、DATABASE(数据库名)、USER(用户名)、ADDRESS(IP地址和掩码)、METHOD(加密方法)
1.type
host:使用TCP/IP 连接
local:使用Unix-domainsocket
hostssl: 指定SSL
hostnossL:禁止SSL
2.database
"all",以及数据库名。对于多个数据库名用逗号隔开
3.use
和database相同
4.address
指定匹配地址
5.METHOD
加密方式:trust,reject,md5,password ......
[root@vm ~]# systemctl restart postgresql-14 #重启服务
[root@vm ~]# su - postgres
Last login: Sun Nov 24 22:05:29 CST 2024 on pts/0
-bash-4.2$ psql
psql (14.15)
Type "help" for help.
postgres=# alter user postgres with password 'postgres';
ALTER ROLE
postgres-# \q
-bash-4.2$ pwd
/var/lib/pgsql
psql(连接数据库,默认用户和数据库都是postgres
# 登录
-bash-4.2$ psql -U postgres -d postgres -h 127.0.0.1 -p 5432
Password for user postgres:
psql (14.15)
Type "help" for help.
# 创建库
postgres=# create database mydb;
CREATE DATABASE
# 在外层 也可以创建库-bash-4.2$ createdb mydb;
# 查看库
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
# 切换库
postgres=# \c mydb
You are now connected to database "mydb" as user "postgres".
mydb=# \c postgres
You are now connected to database "postgres" as user "postgres".
# 删除库
postgres=# drop database mydb;
DROP DATABASE
# 退出
postgres=# \q
# 直接登入
-bash-4.2$ psql -d mydb;
psql (14.15)
Type "help" for help.
mydb=#
建表
主要数据类型:数值数据类型,字符串数据类型,日期/时间数据类型
字符串字符串类型包括
char(size),character(size):固定长度字符串,size规定了需存储的字符数,由右边的空格补齐;varchar(size),charactervarying(size):可变长度字符串,size规定了需存储的字符数;
text:可变长度字符串。
表示日期或时间的数据类型有
timestamp:日期和时间;
date:日期,无时间;
time:时间;
其他数据类型类型还有布尔值boolean(true或false),货币数额money和几何数据等。
# 创建表
mydb=# create table test(id serial primary key,name varchar(255));
#插入数据
mydb=# insert into test(name) values('jack');
INSERT 0 1
# 查询
mydb=# select * from test;
id | name
----+------
1 | jack
(1 row)
# 查看表结构
mydb=# \d test
# 更新数据
mydb=# update test set name='new' where id=1;
UPDATE 1
# 删除
mydb=# delete from test where id=1;
DELETE 1
创建schema 的test表,方便表管理
mydb=# create schema myschema;
CREATE SCHEMA
mydb=# create table myschema.test(id int,name varchar(255));
CREATE TABLE
test where id=1;
DELETE 1
创建schema 的test表,方便表管理
```shell
mydb=# create schema myschema;
CREATE SCHEMA
mydb=# create table myschema.test(id int,name varchar(255));
CREATE TABLE