微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL精简命令:

dos命令行连接Postgresql:

1. 接入Postgresql数据库: psql -h IP地址 -p 端口 -U 用户名 -d 数据库2. 输入数据库密码

分享图片

C:\Users\admin\Desktop
λ psql -U postgres -d jb51.cc_db
用户 postgres 的口令:
psql (11.1)
输入 "help"获取帮助信息.

jb51.cc_db=# \dt
                关联列表
 架构模式 |   名称   |  类型  |  拥有者
----------+----------+--------+----------
 public   | student  | 数据表 | postgres
 public   | student2 | 数据表 | postgres
(2 行记录)
View Code

查看数据库:

1. SELECT datname FROM pg_database; 或者 2. psql命令行中执行:\l (反斜杠l)

例如:

分享图片

jb51.cc_db=# SElECT datname FROM pg_database;
  datname
-----------
 postgres
 template1
 template0
 jb51.cc_db
(4 行记录)


jb51.cc_db=# \l
                                                                             数据库列表
   名称    |  拥有者  | 字元编码 |                      校对规则                       |                        Ct
ype                        |       存取权限
-----------+----------+----------+-----------------------------------------------------+-----------------------------------------------------+-----------------------
 postgres  | postgres | UTF8     | Chinese (Simplified)_Peoples Republic of China.936 | Chinese (Simplified)_Peoples Republic of China.936 |
 template0 | postgres | UTF8     | Chinese (Simplified)_Peoples Republic of China.936 | Chinese (Simplified)_Peoples Republic of China.936 | =c/postgres          +
           |          |          |                                                     |
                           | postgres=CTc/postgres
 template1 | postgres | UTF8     | Chinese (Simplified)_Peoples Republic of China.936 | Chinese (Simplified)_Peoples Republic of China.936 | =c/postgres          +
           |          |          |                                                     |
                           | postgres=CTc/postgres
 jb51.cc_db | postgres | UTF8     | Chinese (Simplified)_Peoples Republic of China.936 | Chinese (Simplified)_Peoples Republic of China.936 | =Tc/postgres         +
           |          |          |                                                     |
                           | postgres=CTc/postgres
(4 行记录)


jb51.cc_db=#
View Code

切换数据库:

切换数据库:\c databasename

分享图片

jb51.cc_db=# SElECT datname FROM pg_database;
  datname
-----------
 postgres
 template1
 template0
 jb51.cc_db
(4 行记录)


jb51.cc_db=# \c postgres;
您现在已经连接到数据库 "postgres",用户 "postgres".
postgres=#
View Code

查看当前数据库中的表:

1. SELECT tablename FROM pg_tables WHERE schemaname=public; # 列出数据库用户自定义的表名 或者 2. psql>\dt

分享图片

jb51.cc_db=# \dt
                关联列表
 架构模式 |   名称   |  类型  |  拥有者
----------+----------+--------+----------
 public   | student  | 数据表 | postgres
 public   | student2 | 数据表 | postgres
(2 行记录)


jb51.cc_db=# SELECT tablename FROM jb51.cc_db;
ERROR:  relation "jb51.cc_db" does not exist
第1行SELECT tablename FROM jb51.cc_db;
                           ^
jb51.cc_db=# SELECT tablename FROM pg_tables WHERE schemaname=public;
 tablename
-----------
 student
 student2
(2 行记录)
View Code

查看库中某个表的结构:

分享图片

使用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 > 0
       and a.attrelid = c.oid
       and a.atttypid = t.oid
 ORDER BY a.attnum
View Code

分享图片

使用命令查询 psql>\d tablename

分享图片

jb51.cc_db-# \d student
                  数据表 "public.student"
   栏位   |      类型      | Collation | Nullable | Default
----------+----------------+-----------+----------+---------
 name     | character(100) |           |          |
 id       | integer        |           |          |
 subjects | character(1)   |           |          |
View Code

 

显示数据的字符:\encoding

jb51.cc_db-# \encoding GBK jb51.cc_db-#

 

修改数据库的表名:

分享图片

1.更改表名 

alter table 表名 rename to 新表名

2.更改字段名

alter table 表名 rename 字段名 to 新字段名

3,更改字段类型

如:ID 字段 原类型为 character varying(50) 新类型为integer

其中,ID中原有数据为1,2,3等数字

用如下语句更改

 alter table dbo.titemtype alter column id type integer using to_number(id,9);
View Code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐