Docker installs the pg database and the basic operations of the pg database

1. First prepare the docker image of the pg database

2. First create a file as an external mount file for pg database data files, configuration files, etc.

3. Create a mirror image

docker run -it -d --name postgres14 --restart=always --privileged=true -p 5432:5432 -e POSTGRES_PASSWORD=postgres -v /home/fengyang/pg_data:/var/lib/postgresql 9f3ec01f884d

In this way, the container is created. Let's take a look at the directory we mounted. There is already a data directory.

Parameter explanation:

-p: port mapping, port 5432 of the physical machine is mapped to port 5432 inside the container

-e: Set the password, which is the password to log in to the database (here two points should be noted, one is to set the password without quotation marks, and the other is that the postgre database has a default user name that is postgres)

-v: file mount, mount the file in the container to the outside of the container, so that if you need to modify something, you don’t have to enter the container again

--restart=always: start automatically at boot

--privileged=true: Give yourself some permission to operate inside the container. In fact, it is not necessary here, but it is just a personal habit

3. Start to operate the database

Enter inside the container:

Enter the database: psql -U username database (the database here can be omitted, if not written, it will enter the postgres library by default) -W press Enter to enter the password, the password is set by -e when creating the container

Fourth, the basic operation of the database

  1. Create database: create database database name;

  1. View all databases: SELECT * FROM pg_database ORDER BY datname

Enter the database: \c database; similarly, the password is set by -e when creating the container.

  1. Create a user: create user username with password 'password';

  1. 查看所有用户: SELECT * FROM pg_roles;

  1. 创建模块: create schema 模块名;

  1. 查看所有模块: SELECT * FROM information_schema.schemata;

  1. 给操作某模块的用户赋权限:GRANT ALL PRIVILEGES ON schema 模块名 TO 用户;

  1. 通过备份文件导入数据:

1) exit; 退出数据库

2)docker cp将备份文件复制到容器中

3)pg_restore -d 数据库 -U 用户名 备份文件; 导入备份文件(注意 如果这里不写-U 用户名 会默认到root用户名下,一般容易报错)。

Guess you like

Origin blog.csdn.net/wangziyang777/article/details/128735036