Record in detail the detailed process of installing postgres data in the Docker environment under macOS, and record the installation steps and problems.
postgres installation
First, use the command to pull the image of postgres
$ docker pull postgers Using default tag: latest latest: Pulling from library/postgres 5eb5b503b376: Pull complete daa0467a6c48: Pull complete 7cf625de49ef: Pull complete bb8afcc973b2: Pull complete c74bf40d29ee: Pull complete 2ceaf201bb22: Pull complete 1255f255c0eb: Pull complete d27501cd0cca: Pull complete ff5b6d09a5d0: Pull complete f635aec27645: Pull complete a165c6729250: Pull complete b0aa4f86b611: Pull complete 9efc4664d9d2: Pull complete Digest: sha256:3162a6ead070474b27289f09eac4c865e75f93847a2d7098f718ee5a721637c4 Status: Downloaded newer image for postgres:latest docker.io/library/postgres:latest
After successful installation, you can build the container
Or check through images
$ docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE postgres latest da2cb49d7a8d 2 weeks ago 374MB portainer/portainer latest 580c0e4e98b0 10 months ago 79.1MB
Record the image ID of posgres da2cb49d7a8d
docker run -it --name postgres --restart always -e POSTGRES_PASSWORD='123456' -e ALLOW_IP_RANGE=0.0.0.0/0 -v /Users/bird/postgres/data:/var/lib/postgresql -p 5433:5432 -d da2cb49d7a8d
- -it: background operation
- -p: Port mapping
- -v: The local directory is mapped to the directory in the container. The local directory / home/postgres/data needs to exist locally
- -e: Environment variable, set the database password, and allow all IP access
Use docker ps to view
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e2f4c61b114 da2cb49d7a8d "docker-entrypoint.s..." 2 seconds ago Up 1 second 0.0.0.0:5433->5432/tcp postgres 47b0e9e81d4f 580c0e4e98b0 "/portainer" 3 hours ago Up 3 hours 0.0.0.0:9000->9000/tcp portainer
At this time, you can see that the container has been running, but the database cannot be accessed. You need to enter the container to modify the configuration file
postgres configuration
Enter the startup container and switch to postgres user login
$ docker exec -it 4e2f4c61b114 bash root@4e2f4c61b114:/# su postgres postgres@4e2f4c61b114:/$
You can see that you have successfully switched to the postgres user. Here is the login by entering the user name and password (the password is the one set through - e when creating the container. I set 123456 here)
postgres@4e2f4c61b114:/$ psql -U postgres -W Password: psql (14.1 (Debian 14.1-1.pgdg110+1)) Type "help" for help. postgres=#
You can see that you have logged into the database. Basically, the installation is successful.
Configure postgres remote access
The postgres database can be used inside the container, but it cannot be accessed outside the container. Recall that we need to modify PG when installing postres database under windows or linux_ hba. conf,postgresql. The operation of conf configuration file is the same in docker.
- Modify postgres configuration file: pg_hba.conf
postgres@4e2f4c61b114:/$ vi /var/lib/postgresql/data/pg_hba.conf bash: vi: command not found
There is no vi command in the prompt of direct modification, so copy the file and modify it
docker cp 4e2f4c61b114:/var/lib/postgresql/data/pg_hba.conf ~/
It should be noted that this command is executed locally in docker, not inside the container. You need to exit the container (if you follow the above steps, you need to exit twice, the first time is to exit postgresql and the second time is to exit the container)
Then you can see the copied file PG locally_ hba. conf
$ vi pg_hba.conf
According to the old rule, change 127.0.0.1 of IPv4 to 0.0.0.0, WQ save it, and then replace this file into the container
# IPv4 local connections: host all all 0.0.0.0/32 trust host all all 0.0.0.0/32 md5
The host parameter indicates the host on which PostgreSQL is installed
All the first all represents all database instances on the host
All the second all represents all users
0.0.0.0/32 indicates the IP address to be connected to the host, and 32 indicates IPV4
md5 indicates the authentication method
$ docker cp ~/pg_hba.conf 4e2f4c61b114:/var/lib/postgresql/data
- Modify the PostgreSQL configuration file: PostgreSQL conf
The specific operation steps are the same as those above. Navigate to #listen_addresses = 'localhost', then # remove the beginning of the line, and change the line content to localhost to: * (I omitted this step here, because the version I installed here allows any host connection request by default)
listen_addresses = '*'
By default, only connection requests from the local localhost are accepted, * allowing the database server to listen for connection requests from any host.