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  #查看schema
 create table n80_sch.t1(id int);  #创建 表属于n80_sch
  \dt n80_sch.t1   #查看
  db1=# \dt n80_sch.t1
         List of relations
 Schema  | 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 
-------
 16411


root@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 ms
 Execution 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: 100000
 Planning Time: 0.058 ms
 Execution Time: 7.986 ms

Time: 8.359 ms



#关闭索引
testdb=#  set enable_indexscan=off;
SET
 set 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

你可能感兴趣的:(postgresql,数据库)