1、安装Postgre SQL客户端工具
brew install postgresql
2、连接到PostgreSQL
(1)创建远程连接
psql -h hostname -U username -d database
其中,hostname 是 PostgreSQL 服务器的主机名或 IP 地址,username 是您的 PostgreSQL 用户名,database 是要连接的数据库名称。
(2)退出连接
-
使用
\q命令:在psql命令行中输入\q,然后按下回车键。这将使您退出数据库连接并返回到命令行提示符。 -
使用快捷键 Ctrl + D:在
psql命令行中按下 Ctrl + D 键组合。这也会导致您退出数据库连接并返回到命令行提示符。
3、PostgreSQL 使用
(1)查询所有数据库名称
SELECT datname FROM pg_database;
返回 PostgreSQL 中所有数据库的名称列表。

(2)查询当前数据库中的所有表名:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

按键盘:Q,返回
当前数据库中 public schema 下的所有表的名称列表。您可以更改 table_schema 的值来查询其他 schema 下的表名。
SELECT table_catalog AS database_name, table_name
FROM information_schema.tables
WHERE table_schema = 'public';

(3)查询表数量
SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';

数据库中存在哪些不同的 table_schema 值
SELECT DISTINCT table_schema
FROM information_schema.tables;
