PostgreSQL精简命令:
2024-08-31 03:16:08
dos命令行连接PostgreSQL:
. 接入PostgreSQL数据库: psql -h IP地址 -p 端口 -U 用户名 -d 数据库名 . 输入数据库密码
C:\Users\admin\Desktop
λ psql -U postgres -d yiibai_db
用户 postgres 的口令:
psql (11.1)
输入 "help" 来获取帮助信息. yiibai_db=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+----------+--------+----------
public | student | 数据表 | postgres
public | student2 | 数据表 | postgres
( 行记录)
查看数据库:
. SELECT datname FROM pg_database;
或者
. psql命令行中执行:\l (反斜杠l) 例如:
yiibai_db=# SElECT datname FROM pg_database;
datname
-----------
postgres
template1
template0
yiibai_db
( 行记录) yiibai_db=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ct
ype | 存取权限
-----------+----------+----------+-----------------------------------------------------+-----------------------------------------------------+-----------------------
postgres | postgres | UTF8 | Chinese (Simplified)_People's Republic of China.936 | Chinese (Simplified)_People's Republic of China. |
template0 | postgres | UTF8 | Chinese (Simplified)_People's Republic of China.936 | Chinese (Simplified)_People's Republic of China. | =c/postgres +
| | | |
| postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_People's Republic of China.936 | Chinese (Simplified)_People's Republic of China. | =c/postgres +
| | | |
| postgres=CTc/postgres
yiibai_db | postgres | UTF8 | Chinese (Simplified)_People's Republic of China.936 | Chinese (Simplified)_People's Republic of China. | =Tc/postgres +
| | | |
| postgres=CTc/postgres
( 行记录) yiibai_db=#
切换数据库:
切换数据库:\c databasename
yiibai_db=# SElECT datname FROM pg_database;
datname
-----------
postgres
template1
template0
yiibai_db
( 行记录) yiibai_db=# \c postgres;
您现在已经连接到数据库 "postgres",用户 "postgres".
postgres=#
查看当前数据库中的表:
. SELECT tablename FROM pg_tables WHERE schemaname='public'; # 列出数据库中用户自定义的表名
或者
. psql>\dt
yiibai_db=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+----------+--------+----------
public | student | 数据表 | postgres
public | student2 | 数据表 | postgres
( 行记录) yiibai_db=# SELECT tablename FROM yiibai_db;
ERROR: relation "yiibai_db" does not exist
第1行SELECT tablename FROM yiibai_db;
^
yiibai_db=# SELECT tablename FROM pg_tables WHERE schemaname='public';
tablename
-----------
student
student2
( 行记录)
查看库中某个表的结构:
使用SQL去查询:
SELECT a.attnum,
a.attname AS field,
t.typname AS type,
a.attlen AS length,
a.atttypmod AS lengthvar,
a.attnotnull AS notnull,
b.description AS comment
FROM pg_class c,
pg_attribute a
LEFT OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid,
pg_type t
WHERE c.relname = 'student'
and a.attnum >
and a.attrelid = c.oid
and a.atttypid = t.oid
ORDER BY a.attnum
使用命令查询
psql>\d tablename
yiibai_db-# \d student
数据表 "public.student"
栏位 | 类型 | Collation | Nullable | Default
----------+----------------+-----------+----------+---------
name | character() | | |
id | integer | | |
subjects | character() | | |
显示数据的字符:\encoding
yiibai_db-# \encoding
GBK
yiibai_db-#
修改数据库的表名/表的字段名/表的字段属性:
.更改表名 alter table 表名 rename to 新表名 .更改字段名 alter table 表名 rename 字段名 to 新字段名 ,更改字段类型 如:ID 字段 原类型为 character varying() 新类型为integer 其中,ID中原有数据为1,,3等数字 用如下语句更改 alter table dbo.titemtype alter column id type integer using to_number(id,'');
、增加一列ALTER TABLE table_name ADD column_name datatype;
、删除一列
ALTER TABLE table_name DROP column_name;
、更改列的数据类型
ALTER TABLE table_name ALTER column_name TYPE datatype;
、表的重命名
ALTER TABLE table_name RENAME TO new_name;
、更改列的名字
ALTER TABLE table_name RENAME column_name to new_column_name;
、字段的not null设置
ALTER TABLE table_name ALTER column_name {SET|DROP} NOT NULL;
、给列添加default
ALTER TABLE table_name ALTER column_name SET DEFAULT expression;
最新文章
- 那些年蹚过的坑(c++)
- 【GoLang】GoLang map 非线程安全 &; 并发度写优化
- js 当前日期及时间
- iOS-多线程--(pthread/NSThread/GCD/NSOperation)--总结
- 【linux】关机重启命令
- eclipse 报错汇总
- 第六十八篇、OC_按照某一字段对数值进行排序
- Redis集群明细文档
- 让 Putty 保存密码,自动登陆的四种方法
- 织梦dedecms5.7后台进去就卡死解决方法
- POJ-1003&;1004
- java学习——入门扫盲篇
- 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!
- eclipse 在win7 64两个图标出现位操作系统无法锁定到任务栏或任务栏
- js 输出HTML 样式
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(999)-如何使用这个系统来开发?
- Python-Struct
- Python 学习笔记03篇
- spring+springmvc+ibatis整合注解方式实例
- java中定时器总结