docker容器中postgres的基本使用

1.进入容器,切换用户,登录密码

[root@192 ~]# docker exec -it postgres bash
root@cbc03f2ff37a:/# su postgres
postgres@cbc03f2ff37a:/$ psql -U postgres -W
Password:
psql (14.5 (Debian 14.5-1.pgdg110+1))
Type "help" for help.

postgres=# 

2.从pqsql中退出:/q

postgres=# \q
postgres@cbc03f2ff37a:/$

3.使用 \l 用于查看已经存在的数据库:

postgres-# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 student   | 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
(4 rows)

postgres-#

4.使用 \c + 数据库名 来进入数据库:

postgres-# \c student
Password:
You are now connected to database "student" as user "postgres".
student-#

5.创建数据库:CREATE DATABASE 库名(除了库名其他两个单词都要大写,不然会报错):记得sql语句后面一定要加“;”

postgres=# CREATE DATABASE teacher;
CREATE DATABASE
postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 student   | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 teacher   | 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
(5 rows)

postgres=#

6.删除数据库:DROP DATABASE 库名

student=# drop database teacher;
DROP DATABASE
student=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 student   | 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
(4 rows)

student=#

7.使用\d来查看数据库下的表格


student=# \d
           List of relations
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 public | xiaohuang | table | postgres
(1 row)

student=#

8.\d tabename查看表格信息

student=# \d xiaohuang
                     Table "public.xiaohuang"
 Column |          Type          | Collation | Nullable | Default
--------+------------------------+-----------+----------+---------
 id     | integer                |           |          |
 name   | character varying(255) |           |          |

student=#

9.删除后表格database table 表名

student=# drop table hh;
DROP TABLE

10.剩下的和mysql差不多

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值