当前位置: 首页 > news >正文

postgresql

源码安装

./configure --prefix=/apps/pgsql
make world -j4
make install-world

useradd -s /bin/bash -m -d /home/postgres postgres
echo -e ‘123456\n123456’ | passwd postgres
mkdir -pv /pgsql/data
chown postgres:postgres /pgsql/data/

设置环境变量

vim /etc/profile.d/pgsql.sh
export PGHOME=/apps/pgsql
export PATH=$PGHOME/bin/:$PATH
export PGDATA=/pgsql/data
export PGUSER=postgres
export MANPATH=/apps/pgsql/share/man:$MANPATH

初始化
su - postgres
initdb -D /pgsql/data/
pg_ctl -D /pgsql/data/ -l logfile start

连接
psql

Ubuntu 启动脚本实现开机自动PostgreSQL
方法一
root@server02:~/postgresql-14.2/contrib# vim start-scripts/linux #修改安装目录和数据目录
prefix=/apps/pgsql

#Data directory
PGDATA=“/pgsql/data”

root@server02:~/postgresql-14.2/contrib# cp start-scripts/linux /etc/init.d/postgresql

root@server02:~/postgresql-14.2/contrib# chmod +x /etc/init.d/postgresql

vi /etc/rc.local
/etc/init.d/postgresql start

方法二
su - postgres -c “/apps/pgsql/bin/pg_ctl -D /pgsql/data/ -l logfile start”

service文件启动

root@server02:/apps/pgsql# cat /lib/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
User=postgres
Group=postgres
ExecStart=/apps/pgsql/bin/postmaster -D /pgsql/data
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target

POSTgresql管理

导入数据:psql -f pg.sql
cat pg.sql
create database luo;
\c luo;
create table test(id int);

配置远程连接

改配置文件,修改监听端口为0.0.0.0
listen_addresses = ‘0.0.0.0’

psql -h 120.77.146.92 -U postgres
psql: error: connection to server at “120.77.146.92”, port 5432 failed: FATAL: no pg_hba.conf entry for host “120.79.60.104”, user “postgres”, database “postgres”, no encryption

配置连接文件,ubuntu yum安装在 /etc/postgresql/14/main/pg_hba.conf
vi pg_hba.conf
host all all 0.0.0.0/0 trust 远程不用密码
host all all 0.0.0.0/0 md5 #要密码,添加此行
重启服务:pg_ctl -D /pgsql/data/ restart

修改用户密码
alter user postgres with password ‘123456’;

远程连接成功
psql -h 120.79.60.104 -U postgres -p 5432

免密连接
cat .pgpass
120.79.60.104:5432:db1:postgres:12345

psql -h 120.79.60.104 db1 postgres

\timing on #显示执行命令时间
\set VERBOSITY VERBOSE ##显示详细的信息,可以打印出报出问题的源代码位置

查看数据库存放目录的路径select oid,datname from pg_database;
create schema  n80_sch;   #创建schema
db1=# \dn  #查看schemacreate table n80_sch.t1(id int);  #创建 表属于n80_sch\dt n80_sch.t1   #查看db1=# \dt n80_sch.t1List of relationsSchema  | Name | Type  |  Owner   
---------+------+-------+----------n80_sch | t1   | table | postgres
b1=#  \c testdb  #切换数据库
testdb=#  insert into tb1 (name) select (md5(random()::text)) from  generate_series (2,10);create table tb2 (like tb1); ##复制表结构,不复制数据
\d tb2  查看表结果select * from pg_tables;  #查看所有的表select pg_total_relation_size('tb1');#查看表大小查看数据库对应的目录
testdb=# select oid,datname from pg_database where datname = 'testdb';oid  | datname 
-------+---------16409 | testdb
(1 row)
查看表对应的文件
testdb=#  select relid from pg_stat_all_tables where relname='tb1';relid 
-------16411root@server02:/pgsql/data/base/16409# ll 16411
-rw------- 1 postgres postgres 8192 Aug 20 12:41 16411或者
testdb=#  select * from pg_relation_filepath('tb1');base/16409/16411查看当前库中的所有表的统计信息select * from pg_stat_all_tables;#查看指定表t1的信息select * from pg_stat_all_tables where relname='t1';

create table tb1 (id serial,name varchar(10));

批量添加

testdb=#  create table tb1 (id serial primary key,name text);
CREATE TABLE
Time: 5.048 ms
testdb=#  insert into tb1 (name) select (md5(random()::text)) from 
generate_series (1,1000000);
INSERT 0 1000000
Time: 3638.546 ms (00:03.639)100万记录2
testdb=#  create table tb1(id int,info text,crt_time timestamp);
CREATE TABLE
Time: 3.263 ms
testdb=#  insert into tb1 select 
generate_series(1,100000),md5(random()::text),clock_timestamp();
INSERT 0 100000显示时间
testdb=# select clock_timestamp();2023-08-20 13:09:20.561662+08#创建索引
testdb=#  create index idx_tb1_id on tb1(id);#查看索引
select * from pg_indexes where tablename='tb1'; 
#查询条件是索引列
testdb=#  explain analyze select * from tb1 where id = 99999;Index Scan using idx_tb1_id on tb1  (cost=0.29..8.31 rows=1 width=45) (actual time=0.020..0.021 rows=1 loops=1)Index Cond: (id = 99999)Planning Time: 0.198 msExecution Time: 0.041 ms
Time: 0.412 ms全表扫描
testdb=# explain analyze select * from tb1 where info = 
'fb16d90265f55880ba96b016afe65b51 ';Seq Scan on tb1  (cost=0.00..2185.00 rows=1 width=45) (actual time=7.967..7.968 rows=0 loops=1)Filter: (info = 'fb16d90265f55880ba96b016afe65b51 '::text)Rows Removed by Filter: 100000Planning Time: 0.058 msExecution Time: 7.986 msTime: 8.359 ms#关闭索引
testdb=#  set enable_indexscan=off;
SETset enable_bitmapscan=off;删除索引
testdb=# drop index idx_tb1_id;

表空间
存放表的目录

postgres@server02:~$ mkdir ts1

testdb=# create tablespace ts1 location ‘/home/postgres/ts1/’;
CREATE TABLESPACE
Time: 1.256 ms
testdb=# \db
pg_default | postgres |
pg_global | postgres |
ts1 | postgres | /home/postgres/ts1

创建表指定表空间
create table tb3(id int) tablespace ts1;

testdb=# select * from pg_relation_filepath(‘tb3’);
pg_tblspc/16448/PG_14_202107181/16409/16449
删除表空间,要先删除表
drop tablespace ts1;

#查看数据库启动时间
postgres=# select pg_postmaster_start_time();

导入数据

psql < hellodb.sql

#关闭自动提交,可以用rollback取消DML语句
\set AUTOCOMMIT off
\set AUTOCOMMIT on

用户账号管理

Create user   user1 PASSWORD '123';CREATE ROLE li WITH LOGIN PASSWORD '123456';

登录
psql -h 120.79.60.104 -U user1 hellodb

创建超级管理员,可以删除库
CREATE ROLE luo WITH SUPERUSER LOGIN PASSWORD ‘123456’ ;

禁止登录
alter user luo with nologin ;

\du 查看用户信息

修改用户有创建数据库权限
postgres=# alter user li with createdb;

授权li用户hellodb数据库查看权限
pinxixi=# \c hellodb
You are now connected to database “hellodb” as user “postgres”.
hellodb=# GRANT select ON ALL TABLES IN schema public to li;
GRANT

安装pgadmin
apt list|grep pgadmin
apt install phppgadmin

http://www.lryc.cn/news/132626.html

相关文章:

  • AutoSAR配置与实践(基础篇)3.2 BSW中的I/O架构和模块详解
  • 基于Java+SpringBoot+Vue的学校田径运动会管理系统【源码+论文+演示视频+包运行成功】
  • 使用 Visual Studio Code Docker 工具调试 .NET 容器
  • AI引擎助力,CamScanner智能高清滤镜开启扫描新纪元!
  • opencv进阶07-支持向量机cv2.ml.SVM_create()简介及示例
  • LA@n维向量@解析几何向量和线性代数向量
  • go 协程并发数控制
  • MySQL的安装以及卸载
  • LRU算法与Caffeine、Redis中的缓存淘汰策略
  • HTML笔记(3)
  • c++——重写(覆盖),实际上对应的就是虚函数
  • 算法通关村——字符串反转问题解析
  • vue + elementui 中 在弹框中使用了 tree型结构(<el-tree></el-tree>),点击关闭按钮按钮重置tree
  • windows adb根据id点击按钮
  • netty(一):NIO——处理消息边界
  • 等保测评--安全计算环境--测评方法
  • open cv学习 (二)色彩空间和通道
  • RS232、RS422、RS485硬件及RS指令、RS2指令应用知识学习
  • 背景属性样式
  • 蓝桥杯每日N题 (消灭老鼠)
  • k8s 用户角色 权限的划分
  • 聊一下操作系统 macOS 与 Linux
  • OJ练习第153题——分发糖果
  • iOS 通知推送服务端部署测试过程详细版
  • 【COMP282 LEC3 LEC4 LEC5】
  • panda3d加载模型复习和python面向对象编程属性学习
  • 使用 Node.js 生成优化的图像格式
  • 【WinAPI详解】<CreateWindowEx详解>
  • 【Git】分支管理
  • 玩转单元测试之gtest