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差不多