【docker容器入门(七)】: Docker 安装postgres
本文主要说一下如何在docker中安装postgre。
1. 启动postgres容器
[root@guoxiaobo ~]# docker run -d \ > --name postgres \ > --restart always \ > -p 5432:5432 \ > -e POSTGRES_PASSWORD=root123 \ > -e PGDATA=/var/lib/postgresql/data/pgdata \ > -v /home/postgres/data:/var/lib/postgresql/data \ > postgres:12 Unable to find image 'postgres:12' locally 12: Pulling from library/postgres 12: Pulling from library/postgres 6ec7b7d162b2: Already exists a7d6065bfd90: Pull complete 1b99004acb42: Pull complete e11ab41f0489: Pull complete eaa59f6a7cf0: Pull complete e27743da9368: Pull complete 10ffdd8ade29: Pull complete bdaa066489bc: Pull complete 889684a529fa: Pull complete a89eb4df2d61: Pull complete 053ae58e1489: Pull complete d2c54ffd195a: Pull complete dc9226611437: Pull complete 3c4b959e7cb8: Pull complete Digest: sha256:95f2184e47035473fc463b5f65254a9b1b770cdb82281a76235b30f335cda2d4 Status: Downloaded newer image for postgres:12 cf05f68d96c72dde70ecb2d735874d392300f6595acadd00c02b3a479207e183
2. 查看postgres容器
[root@guoxiaobo ~]# docker ps | grep postgres cf05f68d96c7 postgres:12 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:5432->5432/tcp postgres
3. 连接postgres
# 进入postgres容器
[root@guoxiaobo ~]# docker exec -it postgres bash
# 连接postgres
root@cf05f68d96c7:/# psql -U postgres
psql (12.5 (Debian 12.5-1.pgdg100+1))
Type "help" for help.
# 创建database
postgres=# create database testdb;
CREATE DATABASE
postgres=# \q
root@cf05f68d96c7:/#
# 查看database
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
(4 rows)
# 切换database
postgres=# \c testdb
You are now connected to database "testdb" as user "postgres".
# 创建表
testdb=# create table userinfo(id int primary key, name char(32), age int);
# 查看表
CREATE TABLE
testdb=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | userinfo | table | postgres
(1 row)
# 插入数据
testdb=# insert into userinfo values(1, 'xw', 18);
INSERT 0 1
# 查看数据
testdb=# select id, name, age from userinfo;
id | name | age
----+----------------------------------+-----
1 | xw | 18
(1 row)
# 断开连接并退出
testdb=# \q
root@cf05f68d96c7:/# exit
exit
欢迎各位光临郭小波的博客,请在此留下您的脚印。

浙公网安备 33010602011771号