基本操作

登陆

psql -h 服务器 -U 用户名 -d 数据库 -p 端口 //-U是大写

数据库操作

# 创建数据库
create database 数据库名 with owner = 所有者 encoding = 编码格式;
create database mydb with owner = postgres encoding = 'utf-8';

# 修改数据库
alter database mydb rename to mydb2; #重命名数据库
alter database mydb connection limit 20; #限制连接数

# 删除数据库
drop database mydb;

# 查看、切换、退出数据库
\l、\c、\q 

数据表操作

#创建表
create table student(
  id serial primary key,
  name varchar(255)
);

#修改数据表
alter table student rename to student1; #修改表名
alter table student rename id to numberID; #修改字段名
alter table student alter column name type varchar(40); #修改字段类型
alter table student drop column name; #删除字段
alter table student add column good varchar(200); #添加字段

#删除表
drop table student;
drop table if exists student;

#查看当前数据库下所有表,查看表结构,相当于 desc
\d、\d student