当前位置:网站首页>笔记系列之docker安装Postgresql 14
笔记系列之docker安装Postgresql 14
2022-07-27 05:01:00 【圆师傅】
文章目录
目标
使用docker-compose安装Postgresql 14,pgadmin4并进行简单的使用。
1. 安装
1.1 docker-compose.ymal
version: "3.5"
services:
postgres:
container_name: pg14
image: postgres:14
environment:
POSTGRES_USER: pg14
POSTGRES_PASSWORD: 123456
PGDATA: /data/postgres
volumes:
- postgres14:/Users/yuanyao/tools/pg
ports:
- "5432:5432"
restart: unless-stopped
pgadmin4:
container_name: pgadmin4
image: dpage/pgadmin4
ports:
- 20001:80
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: 123456
volumes:
postgres14:
在docker-compose.yaml文件所在目录运行如下指令:
docker-compose up -d
如果不在当前目录,那就自己指定需要使用的yaml文件
docker-compose up -f docker-compose.yaml -d
可以看到,这样就已经成功安装好了
注意
- volumes的位置是挂载的数据存方法位置,记得要替换成自己的目录。
- 如果是M1芯片的Mac,image需要替换,可能是这个“arm64v8/postgres:14”。(未经测试)
1.2 pgadmin4
pgadmin4已经启动,那就用起来。由于已经把80端口映射到20001,直接打开localhost:20001端口
这个时候,可以添加server,即添加数据库。在connection界面,最重要的是host地址
注意,这里的host不可以填写本机的localhost。由于docker容器的网络隔离问题,这里需要填写postgres的内部 IP地址。如何获取这个地址?
docker inspect pg14
这个pg14是数据库容器名,可以看到输出的结果包括gateway
可以看到这了的地址是172.20.0.1.将这个地址填入,就可以顺利连接数据库。
2. 使用
2.1 登录 via psql
由于我们已经设置了
POSTGRES_USER: pg14
POSTGRES_PASSWORD: 123456
那么可以使用如下指令登录:
psql -h 127.0.0.1 -p 5432 -d postgres -U pg14
- 这里的-h是指安装pg的地址
- -p 端口号,这里设置的是5432
- -d是数据库的名字,这里默认是会创建一个postgres库
- -U是使用哪个用户登录,这里使用的是创建时指定的pg14
* pg docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c64dc3d68eb2 postgres:14 "docker-entrypoint.s…" 42 hours ago Up 42 hours 0.0.0.0:5432->5432/tcp pg14
* pg docker exec -it pg14 bash
[email protected]:/# cd data/
[email protected]:/data# cd postgres/
[email protected]:/data/postgres# ls
base pg_dynshmem pg_logical pg_replslot pg_stat pg_tblspc pg_wal postgresql.conf
global pg_hba.conf pg_multixact pg_serial pg_stat_tmp pg_twophase pg_xact postmaster.opts
pg_commit_ts pg_ident.conf pg_notify pg_snapshots pg_subtrans PG_VERSION postgresql.auto.conf postmaster.pid
[email protected]:/data/postgres# psql -h 127.0.0.1 -p 5432 -d postgres -U pg14
psql (14.3 (Debian 14.3-1.pgdg110+1))
Type "help" for help.
postgres=#
postgres=# exit
[email protected]:/data/postgres# psql -d postgres -U pg14
psql (14.3 (Debian 14.3-1.pgdg110+1))
Type "help" for help.
postgres=#
本机登录,可以使用默认的IP和端口号。
2.2 CRUD
登录进来后,就要创建数据库,创建表,CURD……
\l 列出所有数据库
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+------------+------------+-------------------
postgres | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
template1 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
(3 rows)
2.2.1 创建数据库
create database pg14;
postgres=# create database pg14;
CREATE DATABASE
postgres=# \l
pg14 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
pgtest | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
template1 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
postgres=# \c pg14;
You are now connected to database "pg14" as user "pg14".
pg14=# \l
pg14 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
pgtest | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | pg14 | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
template1 | pg14 | UTF8 | en_US.utf8 | en_US.utf8 | =c/pg14 +
| | | | | pg14=CTc/pg14
2.2.2 创建表,删除表
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
pg14=# CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE
pg14=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-------
public | company | table | pg14
(1 row)
pg14=# \dt
public | company | table | pg14
pg14=# \d company
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | |
salary | real | | |
pg14=# \d
public | company | table | pg14
pg14=# drop table company;
DROP TABLE
pg14=# \d
Did not find any relations.
2.2.3 基本操作
pg14=# insert into company(id, name, age,address) values(11, 'tom',23,'xxx');
INSERT 0 1
pg14=# select * from company;
11 | tom | 23 | xxx |
pg14=# insert into company(id, name, age,address) values(12, 'Jerry',23,'xxx');
INSERT 0 1
pg14=# select * from company;
11 | tom | 23 | xxx |
12 | Jerry | 23 | xxx |
pg14=# update company set name = 'TOM' where name = 'tom';
UPDATE 1
pg14=# select * from company;
12 | Jerry | 23 | xxx |
11 | TOM | 23 | xxx |
pg14=# ALTER TABLE company add email VARCHAR(40);
ALTER TABLE
pg14=# \d company;
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | |
salary | real | | |
email | character varying(40) | | |
pg14=# ALTER TABLE company drop column salary;
ALTER TABLE
pg14=# \d company;
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | |
email | character varying(40) | | |
pg14=# ALTER TABLE company RENAME TO great_company;
ALTER TABLE
pg14=# \d
public | great_company | table | pg14
pg14=#
3. 权限
创建用户并赋予权限;撤销权限并删除用户;
CREATE USER test WITH PASSWORD ‘123456’;
GRANT ALL ON great_company to test;
revoke all on great_company from test;
drop user test;
pg14=# CREATE USER test WITH PASSWORD '123456';
CREATE ROLE
pg14=# \du test
test | | {
}
pg14=# GRANT ALL ON great_company to test;
GRANT
pg14=# revoke all on great_company from test;
REVOKE
pg14=# drop user test;
DROP ROLE
4. 索引
4.1 单列索引
CREATE INDEX index_name
ON table_name (column_name);
4.2 组合索引
CREATE INDEX index_name
ON table_name (column1_name, column2_name);
4.3 唯一索引
使用唯一索引不仅是为了性能,同时也为了数据的完整性。唯一索引不允许任何重复的值插入到表中。
CREATE UNIQUE INDEX index_name
on table_name (column_name);
4.4 什么情况下要避免使用索引?
虽然索引的目的在于提高数据库的性能,但这里有几个情况需要避免使用索引。
使用索引时,需要考虑下列准则:
索引不应该使用在较小的表上。
索引不应该使用在有频繁的大批量的更新或插入操作的表上。
索引不应该使用在含有大量的 NULL 值的列上。
索引不应该使用在频繁操作的列上。
5. TRUNCATE 语句
TRUNCATE TABLE语句的用途是清空表内容。不带WHERE条件子句的 DELETE语句也表示清空表内容,从执行结果来看,两者实现了相同的 功能,但两者实现的原理是不一样的。TRUNCATE TABLE语句是DDL语 句,即数据定义语句,相当于用重新定义一个新表的方法把原表的内 容直接丢弃了,所以TRUNCATE TABLE执行起来会很快;而DELETE语句 是DML语句,我们可以认为DELETE语句是把数据一条一条地删除,所以 DELETE语句删除多行数据时执行起来比较慢。
6. 关闭自动提交
在psql中关闭自动提交功能
\set AUTOCOMMIT off
这个命令中的“AUTOCOMMIT”是大写的,不能使用小 写,如果使用小写,虽不会报错,但会导致关闭自动提交的操作无 效。
边栏推荐
- Deep Qt5 signal slot new syntax
- Svn usage details
- 35. Scroll
- Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
- ssm框架整合
- [Niuke discussion area] Chapter 7: building safe and efficient enterprise services
- Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
- Acceptance and neglect of events
- pyside2____ 1. Installation and case listing
- Solution and principle analysis of feign call missing request header
猜你喜欢

来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”

Svn usage details

树莓派rtmp推流本地摄像头图像

如何快速有效解决数据库连接失败问题

Install pyGame

Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)

整合SSM

JVM Part 1: memory and garbage collection part 7 -- runtime data area heap

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

ERP system brand
随机推荐
Flexible array and common problems
Card drawing program simulation
Detailed explanation of pointer constant and constant pointer
抽卡程序模拟
How to test the payment process?
Introduction to dynamic memory functions (malloc free calloc realloc)
Deep Qt5 signal slot new syntax
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
Demo of throttling function -- regular expression matching
微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”
Mysql表的约束
Alphabetic order problem
二、MySQL高级
B1021 个位数统计
精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?
Differences among left join, inner join and right join
JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
Invert a Binary Tree
Solution and principle analysis of feign call missing request header