Docker installs PostgreSQL and PostGIS

Ashamed to say, the famous open source database PostgreSQL is used for the first time. Because it needs to store geographic information data, it also uses the PostGIS plug-in to record the installation and configuration process in the Docker environment.


Installation Environment

Docker Desktop for Windows 4.10
PostgreSQL 10.19
PostGIS 10.3


1. Install PostgreSQL

Pull to mirror

docker pull postgres 10.19

Create and start the container

docker run -d --name mypostgres --restart=always -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e PGDATA=/var/lib/postgresql/data/pgdata -v /custom/mount:/var/lib/postgresql/data postgres:10.19

The meanings of several parameters are as follows:

–name mypostgres | container name
–restart=always | always restart automatically
-p 5432:5432 | port mapping (host Host port: container port)
-e POSTGRES_PASSWORD=postgres | custom parameter (password)
-e PGDATA=/var/lib /postgresql/data/pgdata | custom parameters (data storage path)
-v /custom/mount:/var/lib/postgresql/data | path mapping

2. Install PgAdmin

docker run -p 5050:80 -e '[email protected]' -e 'PGADMIN_DEFAULT_PASSWORD=admin' -d --name pgadmin4 dpage/pgadmin4

Since the port has been mapped to 5050, use a browser to access http://localhost:5050, and connect to PostgreSQL after logging in
insert image description here

3. Install PostGIS

Here, install PostGIS in the docker container
to enter the postgresql container

docker exec -it postgres bash

Use apt to install postgis. Since the version used by PostgreSQL is the 10 series, PostGIS also uses the corresponding version.

apt-get install postgresql-10-postgis-3 postgresql-10-postgis-3-dbgsym postgresql-10-postgis-3-scripts

insert image description here

Using apt-get sometimes fails to install, prompting that the corresponding package or dependent library cannot be found, and apt needs to be updated first

apt update   //更新apt
apt-get update  //更新apt-get

In addition, you can also use yum to install postgis, or use wget to download the postgis compressed package to decompress and run, but you need to install and update the components

apt install yum
apt install wget

4. Data Migration

4.1. Backup data

Open pgAdmin, right-click on the database that needs to back up data and select "Backup", select or enter the name of the backup file, select other parameters according to the situation, and click Backup to start execution.
insert image description here
The corresponding command is:

/usr/local/pgsql-10/pg_dump --file "/var/lib/pgadmin/storage/admin_admin.org/backup-20220706" --host "192.168.199.38" --port "5432" --username "postgres" --no-password --verbose --role "postgres" --format=c --blobs --encoding "UTF8" "GISDB"

4.2. Restoring data

Open pgAdmin, right-click on the database that needs to restore data and select "Restore", select the name of the backup file, and click Restore to start execution.
insert image description here
The corresponding command is:

/usr/local/pgsql-10/pg_restore --host "host.docker.internal" --port "5432" --username "postgres" --no-password --role "postgres" --dbname "GISDB" --verbose "/var/lib/pgadmin/storage/admin_admin.org/backup-20220706"

Guess you like

Origin blog.csdn.net/lordwish/article/details/125710982