文章目录
在psql shell的提示下输入对应信息登录
psql.exe -h 服务器IP -U 登录名 -d 数据库名 -p 端口
显示所有数据库列表
\l ; (注意这里是小写的L, 注意空格)
建立名为abc的数据库
create database abc ;
打开并连接abc数据库
\c abc ;
在数据库abc中建立名为posts的表
create table posts (title varchar(255), content text) ;
命令成功后会显示CREATE TABLE
查看表格语法
\dt ;
查看第五条建立的表格的内容语法
\d posts ;
使用sql脚本语法
\i d:/db.sql ;
建立了一个内容为create table posts (title varchar(255), content text); 的sql脚本,并命名为db.sql, 而后将该脚本放置于postgresql的安装所在盘根目录下, 比如D盘对应路径为D:\db.sql
删除表格posts语法
drop table posts ;
删除数据库abc语法
drop database abc ;(注意删除前先退出abc数据库, 可以用\c切换到别的数据库中,如\c postgres;)
重命名表格posts语法
alter table posts rename to new ;
退出语法
\q ;