postgresql-docker installation

Installation environment: centos7.7
1. Query available docker images
docker search postgres
2. We choose docker.io/postgres, the highest-star download
docker pull docker.io/postgres (if you don’t add the version number here, the latest download will be downloaded by default Yes, if you add the version number docker pull docker.io/postgres:10, it is to download the specified version)
3. View the local image
docker image ls
4. Start the container, here we start the image with version number 10, and use it IMAGE ID to start
docker run -v /storage/postgresql/data:/var/lib/postgresql/data --restart=always --privileged=true --name postgresql13 -e POSTGRES_PASSWORD=sinnrenadmin -p 5432:5432 c96f8b6bc0d9
run Start the container
--name postgresql10 means to name the started container postgresql10
POSTGRES_PASSWORD=sa means to set the password for pg's default user postgres to sa
5432: 5432 means to map the port of the machine to the port of the container
b62e41d8ddcc means to start the name b62e41d8ddcc mirroring
If you want to restart the container as docker followed restart, please add --restart = always, that is, below this
docker run --restart=always --name postgresql10 -e POSTGRES_PASSWORD=sa -p 5432:5432 -d b62e41d8ddcc In
most cases, to start the database container, you need to mount the data volume outside the container, so the container is deleted , The data will not be lost:
docker run -v /storage/postgresql/data:/var/lib/postgresql/data --restart=always --privileged=true --name postgresql13 -e POSTGRES_PASSWORD=sinnrenadmin -p 5432:5432 c96f8b6bc0d9
Note that permission --privileged=true must be given here, otherwise it will prompt failure. -v /home/pg10/data:/var/lib/postgresql/data means to mount the data file of the container to the /home/pg10/data directory of Linux.
There is a parameter -d, the function of this parameter is to start from the background. If you don't add it, the foreground is started.

    5.查看容器
    docker ps -a
    6、进入指定容器
                            docker exec -it 775c7c9ee1e1 /bin/bash 或 docker exec -it postgres psql -U postgres
                            7、启动docker   docker start <id或名称>
                            8、停止docker   docker stop <id或名称>

Operate pg database
2, log in to the database:
psql -U postgres
3, modify the password:
ALTER USER postgres with encrypted password'sinnrenadmin';
here, set the password of user postgre as root, and you can modify it according to your needs.
4. Exit the database:
\q

Other commonly used commands:
\h: View the explanation of SQL commands, such as \h select.
\?: View the list of psql commands.
\l: List all databases.
\c [database_name]: Connect to other databases.
\d: List all tables in the current database.
\d [table_name]: List the structure of a certain table.
\du: List all users.
\e: Open a text editor.
\conninfo: List current database and connection information.

Create a user:
create user test with password'rong';
create a user database
CREATE DATABASE testdb OWNER test;
assign all permissions of the testdb database to test:
GRANT ALL PRIVILEGES ON DATABASE testdb TO test;
delete the database:
DROP DATABASE dbname
modify the database owner :
Alter database "srdb" owner to "ffa";

Guess you like

Origin blog.51cto.com/xuanxy/2544430