当前位置:   article > 正文

docker运行postgresql

docker运行postgresql

启动,挂载数据目录

docker run --name postgis_dev -e POSTGRES_PASSWORD=123456 -v /Users/cardiac/Docker/pg_data01:/var/lib/postgresql/data -p 5432:5432 -d mdillon/postgis

注: -v: 前面的路径是本地系统的路径,冒号":" 后面的路径是postgresql运行的路径,配置文件可以放在本地路径下,并且可以更改配置文件中的参数。

进入bash命令行

docker exec -it postgis_dev bash

关闭容器

docker stop postgis_dev

启动容器

docker start postgis_dev

 

使用psql查看数据库,注意:需要切换一下用户。

  1. root@e2452813de70:/# psql -l psql: FATAL: role "root" does not exist
  2. root@e2452813de70:/# su postgres
  3. postgres@e2452813de70:/$ psql -l
  4. List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges
  5. ------------------+----------+----------+------------+------------+-----------------------
  6. mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
  7. postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
  8. template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
  9. | | | | | postgres=CTc/postgres
  10. template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
  11. | | | | | postgres=CTc/postgres
  12. template_postgis | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
  13. vicky | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
  14. (6 rows)

 

命令行登录postgresql数据库

  1. root@e2452813de70:/# psql -U postgres -W
  2. Password:
  3. psql (11.2 (Debian 11.2-1.pgdg90+1))
  4. Type "help" for help.

 

-U 指定用户

-W 使用密码登录

 

psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称]

举例: psql -h 192.168.1.121 -p 5432 vicky postgres

 

登录数据库后,

列出所有数据库: \list

postgres=# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------------+----------+----------+------------+------------+----------------------- mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 | 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 template_postgis | postgres | UTF8 | en_US.utf8 | en_US.utf8 | vicky | postgres | UTF8 | en_US.utf8 | en_US.utf8 | (6 rows)

 

切换数据库: \c 数据库名称

postgres=# \c vicky

 

查看所有表 \d (注意:在docker里面启动的环境,使用\d会报 Did not find any relations. 并没有列出所有表)

vicky=# \d Did not find any relations.

 

查看对象结构,对象可以是表,索引,视图等

\d (表名,索引名称,视图)

vicky=# \d ods.my_concubine Table "ods.my_concubine" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | | name | character varying(50) | | | age | integer | | |

 

查看对象的详细信息: \d+ (表名,索引名称,视图)

vicky=# \d+ ods.my_concubine

Table "ods.my_concubine"

Column | Type | Collation | Nullable | Default | Storage | Stats target | Description --------+-----------------------+-----------+----------+---------+----------+--------------+-------------

id | integer | | | | plain | |

name | character varying(50) | | | | extended | |

age | integer | | | | plain | |

列出所有的schema

vicky=# \dn

List of schemas Name | Owner

--------+----------

ods | postgres

public | postgres

(2 rows)

 

列出所有的表空间

vicky=# \db List of tablespaces Name | Owner | Location

------------+----------+----------

pg_default | postgres |

pg_global | postgres | (2 rows)

 

列出数据库的所有角色或用户

vicky=# \dg

List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+-----------

postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

vicky=# \du

List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+-----------

postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

 

列出表的权限分配

vicky=# \dp ods.my_concubine

Access privileges

Schema | Name | Type | Access privileges | Column privileges | Policies --------+--------------+-------+-------------------+-------------------+----------

ods | my_concubine | table | | |

(1 row)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/464750
推荐阅读
相关标签
  

闽ICP备14008679号