pgsql基本操作

查看已经存在的数据库

复制代码
postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 runoobdb  | postgres | UTF8     | C       | C     | 
(4 rows)

进入数据库:

复制代码
postgres=# \c runoobdb
You are now connected to database "runoobdb" as user "postgres".

连接数据库

复制代码
$ psql -h localhost -p 5432 -U postgres runoobdb
Password for user postgres: ****
psql (11.3)
Type "help" for help.
You are now connected to database "runoobdb" as user "postgres".
runoobdb=# 

删除数据库

复制代码
postgres=# DROP DATABASE runoobdb;	

查看所有表

复制代码
\d

查看表信息

复制代码
runoobdb=# \d company
                  Table "public.company"
 Column  |     Type      | Collation | Nullable | Default 
---------+---------------+-----------+----------+-------

删除表

复制代码
runoobdb=# drop table department

12.PostgreSQL 导出数据库:(以数据库名为work_face为例)

复制代码
pg_dump -h 127.0.0.1 -p 5432 -U postgres -d work_face  -Fc -f C:\Users\admin\Desktop\work_face.dmp 

PostgreSQL 导入数据库:(以数据库名为work_face为例)

复制代码
pg_restore -h 127.0.0.1 -U postgres -d work_face C:\Users\admin\Desktop\work_face.dmp

查看当前活动

复制代码
SELECT  pid,  usename AS username,  datname AS database_name,  application_name AS application,  state,  query FROM  pg_stat_activity;

查看指定表索引信息

复制代码
SELECT    indexname AS index_name,    indexdef AS index_definition FROM pg_indexes WHERE tablename = 'event_log';
相关推荐
码农阿豪8 小时前
上线前能跑,上线后挂了,三个隐性的SQL陷阱
数据库·sql
乖巧的妹子9 小时前
SQL知识点
数据库·sql·oracle
xd18557855513 小时前
SQL语句生成-基于鸿蒙的AI SQL语句生成应用开发实践
人工智能·sql·华为·harmonyos·鸿蒙
xieliyu.14 小时前
事务原子性、一致性、隔离性、持久性通俗讲解
android·sql·mysql
G.O.G.O.G1 天前
LeetCode SQL 从入门到精通(MySQL)06(上)
数据库·sql·mysql·leetcode
G.O.G.O.G1 天前
《LeetCode SQL 从入门到进阶(MySQL)》06(下)
数据库·sql·oracle
heimeiyingwang1 天前
【架构实战】SQL注入与XSS防御:常见Web漏洞的系统性修复
前端·sql·架构
ClouGence1 天前
跨云、跨地域数据同步,这样更省心
数据库·sql·saas
Csvn2 天前
📊 SQL 入门 Day 4:聚合函数 — COUNT / SUM / AVG / MIN / MAX
后端·sql
哈__2 天前
MCP 协议落地实践:国产化数据库 AI 一站式 SQL 诊断调优方案
数据库·人工智能·sql