• postgres基本操作大全


    首先切换到postgres用户

    su - postgres -- 首先切换到postgres

    常识:PG安装完后默认带有postgres库,可以先进入postgres库再去创建新库

    1创建用户:

    postgres=# create user  testuser  with  password '123456';

    CREATE ROLE

    2创建数据库:

    postgres=# create database  testdb  owner  testuser;     创建数据库给指定用户

    3将数据库的权限全部赋给用户:

    postgres=# grant all  on database  testdb  to testuser;

    4创建schema:

    postgres=# create schema  abc  authorization abcuser;

    create schema

    postgres=# create schema authorization abcuser; 指定了owner则schema名字和owner一致

    5删除schema  

    drop  schema  abc  cascade;     加了cascade可以把关联的表和视图一起删掉

    6 修改数据库密码

    alter user abcuser with password '123' 

    7导入整个数据库:

    q先退出数据库编辑模式

    psql -U testuser   testdb <  /data/backup.sql    用户名和数据库

    8远程连接和导入命令:

    psql  -h  主机域名或者IP   -p  端口  -U 用户  -d  数据库     (PG默认端口5432)

    例如:psql  -h postgres.db.com   -p 5432  -U testuser  -d  testdb

    psql  -h postgres.db.com   -p 5432  -U testuser  -d  testdb  <  table_create.utf8.sql

    9切换数据库

    c dbname  (相当于mysql 的  use dbname)

    10列举数据库

    l (相当于mysql 的show databases)

    11列举表

    dt (相当于mysql 的show tables)

    12查看表结构

    d tblname (相当于mysql 的 desc  tblname   ,   show columns from tbname)

    13查看索引

    di

    表操作:

    1改变表名

    alter   table  [表名A]   rename  to [表名B];

    2删除表

    drop table  [表名];

    3表里添加字段

    alter  table [表名]  add column  [字段] [类型];

    4删除表字段

    alter table  [表名]  drop column [字段];

    5重命名一个表字段

    alter  table [表名]   rename column [A] to [B];

    6表中插入数据

    insert  into 表名 (字段名m) values (列m的值)

    7更新表中某行某列的数据

    update  [表名]  set  [目标字段名]=[目标值] where [改行特征];

    8删除表中某行数据

    delete  from 表名 where [改行特征];

    9删除整个表

    delete from  表名;

    10创建表

    create table  字段名  类型;

    角色创建:

    1create  role  rolename  with optional_permisson;

    2create  role  rolename with login;

    h  create role 可查看创建角色时可设置的管理权限

    3改变角色的权限“

    alter  role rolename with attribute_options;

    alter  role rolename with nologin;

    4赋予角色权限

    grant permission_type on  tablename  to rolename;

    备注:

    copyright  显示PG的使用和发行条款

    encoding  [字元编码名称]

    password  [username]

    q 退出

    create role  rolename 和 create user username这两者的区别:创建的角色rolename没有登录权限,而创建的用户username是有登录权限的,

    du显示用户和用户的属性

    pg_ctl reload重启PG

  • 相关阅读:
    【SSH】——Hibernate三种状态之间的转化
    【工具学习】——教你读懂Maven的配置文件
    【SSH】——封装参数不确定的分页查询
    从零开始配置Jenkins(二)——常见问题及排错思路
    从零开始配置Jenkins(一)——基本配置
    Chrome插件集合
    Vue.js——vue-resource全攻略
    vue中用qs传参发送axios请求
    vue项目打包后首页一片空白解决办法和具体原因总结
    vue开发环境和生产环境里面解决跨域的几种方法
  • 原文地址:https://www.cnblogs.com/wangnengwu/p/12432248.html
Copyright © 2020-2023  润新知