[root@localhost ubuntu]# docker ps|grep pg 1.查询需要进入的pg容器信息
[root@localhost ubuntu]# docker ps
ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
158ee41a55c8 postgres:10.17 "docker-entrypoint.s…" 4 hours ago Up 4 hours 5432/tcp peaceful_cartwright
[root@localhost ubuntu]# docker ps|grep pos
158ee41a55c8 postgres:10.17 "docker-entrypoint.s…" 5 hours ago Up 5 hours 5432/tcp peaceful_cartwright
[root@localhost ubuntu]# docker exec -it peaceful_cartwright /bin/sh 2.进入指定的容器
# su - postgres 3.切换到数据库postgres用户
postgres@158ee41a55c8:~$ psql 4.切换到psql界面
psql (10.17 (Debian 10.17-1.pgdg90+1))
Type "help" for help.
postgres=# select datname from pg_database; 5.查询数据库
datname
-----------
postgres
template1
template0
(3 rows)
postgres=# c postgres 6.进入指定的数据库
You are now connected to database "postgres" as user "postgres".
postgres=# select version(); 7.执行相关的实际工作
version
------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 10.17 (Debian 10.17-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
(1 row)
postgres=# q 8.退出psql
postgres@158ee41a55c8:~$ exit 9.退出postgres
logout
# exit
[root@localhost ubuntu]#



