Database as a part of the project must be a lot of friends in daily development or private work have to contact, and Oracle, PostgresQL and other databases are relatively more complex and cumbersome.

1. Install the database

1. Pull the mirror

Docker pull postgres:13.0Copy the code

2. Start the container

docker run --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -v / usr/local/postgresql/data: / var/lib/postgresql/data - d postgres: 13.0Copy the code

Parameter description:

  • Run: Creates and runs a container;
  • –name postgres: specifies the name of the container to be created;
  • -e POSTGRES_PASSWORD=password: Sets environment variables and specifies the database login password as password.
  • -p 5432:5432: port mapping Maps port 5432 of the container to port 5432 of the external machine.
  • – v/usr/local/postgresql/data: / var/lib/postgresql/data will be/usr/local/postgresql/data is mounted to the container of/var/lib/postgresql/data file, There’s a saying that these two folders have the same data.
  • -d postgres Specifies that postgres is used as the mirror.

The initial user is postgres. The initial password is password

3. View the container

docker ps 
Copy the code

4. Create a tablespace

When Docker creates a tablespace using PostgresQL, go to the container and create a folder as user postgres

Docker exec -it container image /bin/bash su - postgres mkdir system PSQL database user name CREATE TABLESPACE Name of the tablespace owner Owner location 'Path name in the container '; Example: the create in tablespace systme owner admin location '/ var/lib/postgresql/data/system'; You are advised to create a folder under /var/lib/postgresql/data (system is created here) each time you create a tablespace, because a PG_13_ date folder will be created in the directory, which may be repeatedCopy the code

Abnormal 5.

If the following exception is thrown

Changing ownership of '.': Permission denied or chmod: changing permissions of '/var/lib/postgresql/data': Permission deniedCopy the code

SELinux needs to be turned off

Getenforce # View SELinux status Permissive closed enforcing Enable status SetenForce 0 # Temporarily closed Setenforce 1 # Temporarily closed // Permanently closed Modify /etc/selinux/config SELINUX = instead of enforcing the SELINUX = disabledCopy the code

When you want to create a folder that you want in your database, you must enter the container and use the fixed user postgres to create it, otherwise you will throw an exception without permission

2. The installation uses a visual interface

Docker also provides a visual interface. It is recommended to use portainer. You can select a container type and install it directly.

1. Download the image

docker pull portainer/portainer
Copy the code

2. Sinicize Chinese

If you need to be Chinese, you can download it on the web disk:

  • Link: pan.baidu.com/s/1ExFIxfg6…
  • Extract code: VSB4 \
  • Decompress it and put it under the root directory, as shown in the following figure.

3. Start

docker run -d -p 9000:9000 --restart=always  -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data -v /public:/public --name portainer  portainer/portainer
Copy the code

Parameter description:

  • -d: The container runs in the background.
  • -p 9000:9000: indicates the 9000 port in the host 9000 port mapping container
  • – The restart flag checks the container’s exit code and uses it to decide whether to restart the container. By default, the container does not restart.
  • – restart=always: automatically restarts the container
  • – v/var/run/docker. The sock: / var/run/docker. The sock: the host machine’s docker daemon (docker daemon) default listening Unix domain socket mounted into the container
  • -v /public:/public: maps the host public folder to the Docker container
  • – Name portainer: Name the container portainer

4. Login

Login IP address :9000 (port enabled above) You need to set a password for the first login.Select the localThen you can go to the page.