加州大学开发的开源数据库管理系统
- 与mysql的基本操作差不多
- 终端命令多用\d等
- 重点要找与mysql不同的地方
优点:
- 支持
下载和安装
到网址:https://postgresapp.com/downloads.html 下载源文件
按傻瓜方式安装之后有🐘头像
启动台:点击大象,initiate
查看postgresql.conf, 其中有host, port
终端连接数据库
psql -h "localhost" -p 5432 ## 默认没有用户名密码
客户端基本命令
postgres=# \help // 获取SQL命令的帮助,同 \h
postgres=# \quit // 退出,同 \q
postgres=# \password dlf // 重新设置用户dlf的密码,然后需要 \q退出后才生效
c:\>psql exampledb < user.sql // 将user.sql文件导入到exampled数据库中
postgres=# \h select // 精细显示SQL命令中的select命令的使用方法
postgres=# \l // 显示所有数据库
postgres=# \dt // 显示当前数据库中的所有表
postgres=# \d [table_name] // 显示当前数据库的指定表的表结构
postgres=# \c [database_name] // 切换到指定数据库,相当于use
postgres=# \du // 显示所有用户
postgres=# \conninfo // 显示当前数据库和连接信息
postgres=# \e // 进入记事本sql脚本编辑状态(输入批命令后关闭将自动在命令行中执行)
postgres=# \di // 查看索引(要建立关联)
postgres=# \prompt [文本] 名称 // 提示用户设定内部变数
postgres=# \encoding [字元编码名称] // 显示或设定用户端字元编码
*可以将存储过程写在文本文件中aaa.sql,然后在psql状态下:
postgres=# \i aaa.sql // 将aaa.sql导入(到当前数据库)
postgres=# \df // 查看所有存储过程(函数)
postgres=# \df+ name // 查看某一存储过程
postgres=# select version(); // 获取版本信息
postgres=# select usename from pg_user; // 获取系统用户信息
postgres=# drop User 用户名 // 删除用户
创建数据库
create database dbname;
表的操作
-- 新建表
create table tb2 (id int not null primary key, name varchar(30));
-- 插入数据, 特别注意只能使用单引号
insert into tb2 (id,name) values(1,'good');
-- 更新数据
update tb2 set name='武佩奇' where id=3;