PostgreSQL 常用命令

更新日期: 2023-04-18 阅读次数: 1104 字数: 331 分类: postgre

PostgreSQL 的命令相对 MySQL 还是有点难记。单词缩写不太直观。

列出所有数据库

postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 test_db1  | test_db1 | UTF8     | C.UTF-8 | C.UTF-8 | =Tc/test_db1         +
           |          |          |         |         | test_db1=CTc/test_db1
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)

连接一个数据库

postgres=# \c test_db1
You are now connected to database "test_db1" as user "postgres".

列出所有表

test_db1=# \dt
Did not find any relations.

test_db1=# \dt
                List of relations
 Schema |         Name         | Type  |  Owner
--------+----------------------+-------+----------
 public | cwmp_config          | table | test_db1
 public | cwmp_config_session  | table | test_db1
 public | cwmp_factory_reset   | table | test_db1
 public | cwmp_firmware_config | table | test_db1
 public | cwmp_preset          | table | test_db1
 public | cwmp_preset_task     | table | test_db1
 public | net_cpe              | table | test_db1
 public | net_cpe_param        | table | test_db1
 public | net_node             | table | test_db1
 public | sys_config           | table | test_db1
 public | sys_opr              | table | test_db1
 public | sys_opr_log          | table | test_db1
(12 rows)

新建用户

CREATE USER test_db1 WITH PASSWORD 'test_db1'

新建数据库

CREATE DATABASE test_db1 OWNER test_db1;
GRANT ALL PRIVILEGES ON DATABASE test_db1 TO test_db1;

修改/设备用户密码

如果新建的 PostgreSQL 用户没有设置密码,可以后续通过命令行设置密码。

alter user test_db1 with encrypted password 'test_db1';

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式