postgres日常操作
1.启动pgsl数据库
[postgres@master ~]$ pg_ctl start
[postgres@master data]$ pg_ctl -D /usr/local/pgsql/data start
2.查看pgsql版本
[postgres@master ~]$ pg_ctl --version
3.命令行登录数据库
psql -U username -d dbname -h hostip -p port
[postgres@master ~]$ psql -U zhang -d mydb -h 127.0.0.1 -p 5432
psql (10.5)
Type "help" for help.
mydb=>
4.列出所有数据库
[postgres@master ~]$ psql -U zhang -d mydb -h 127.0.0.1 -p 5432
psql (10.5)
Type "help" for help.
mydb=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
(5 rows)
5.切换数据库
mydb=> \c test
You are now connected to database "test" as user "zhang".
6.指定表的所有字段
mydb=> \d employees
Table "public.employees"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
age | integer | | |
mydb=>
7.查看指定表的基本情况
mydb=> \d+ employees
Table "public.employees"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
id | integer | | | | plain | |
age | integer | | | | plain | |
mydb=>
8.新建表
create table TESTCASE(
id INTEGER,
task_class INTEGER,
age TEXT,
PRIMARY KEY(id, task_class)
);
9.自增SERIAL
create table CREATETASK_CHKID_N(
id SERIAL PRIMARY KEY,
chk_id TEXT,
n INTEGER
);
#生成一个以表名_id_seq的序列
mydb=> \d createtask_chkid_n_id_seq
Sequence "public.createtask_chkid_n_id_seq"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
---------+-------+---------+------------+-----------+---------+-------
integer | 1 | 1 | 2147483647 | 1 | no | 1
Owned by: public.createtask_chkid_n.id
#查看下一个值
mydb=> select nextval('createtask_chkid_n_id_seq');
nextval
---------
1
(1 row)
#插入数据
insert into CREATETASK_CHKID_N values(nextval('createtask_chkid_n_id_seq'),'zhang',10)
10.删除表
drop table CREATETASK_CHKID_N
11.清空表
truncate table TESTCASE;
delete from TESTCASE;
12.添加字段
alter table [表名] add column [字段名] [类型];
alter table testcase add column rate_term numeric(5,0);
13.更改字段名称
alter table [表名] rename column [旧字段名] to [新字段名]; ALTER TABLE testcase rename column rate_term to rate;
14.修改列属性
ALTER TABLE testcase ALTER COLUMN rate TYPE integer;
15.执行SQL文件
psql -h localhost -d databaseName -U username -f filename
psql -h 127.0.0.1 -d mydb -U zhang -f testcase.sql
16. 查询结果存储到输出文件
mydb=> \o /home/postgres/exp_testcase.dat
mydb=> select id from testcase;
mydb=> \q
[postgres@master ~]$ more exp_testcase.dat
id
----
1
2
(2 rows)
postgres日常操作的更多相关文章
- LINUX日常操作二
参见:Linux日常操作一 selinux 开启和关闭 一.查看SELinux状态:1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled ...
- ORACLE日常操作手册
转发自:http://blog.csdn.net/lichangzai/article/details/7955766 以前为开发人员编写的oracle基础操作手册,都基本的oracle操作和SQL语 ...
- Oracle 11g 物理Dataguard日常操作维护(二)
Oracle 11g 物理Dataguard日常操作维护(二) 2017年8月25日 14:34 3.3 3.3.1 查看备库进程状态 SYS(125_7)@fpyj123> select pr ...
- redis日常操作
redis针对所有类型的日常操作: keys * ## 取出所有key keys my* ## 模糊匹配 exists name ## 存在name键返回1,否则返回0 del key1 ## 删除一 ...
- 从零开始使用git第二篇:git的日常操作
从零开始使用git 第二篇:git的日常操作 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:git撤销操作.分支操 ...
- python专题我对json的日常操作
一前言 本篇文章将会阐述对json的日常操作,如何读取json文件,将json文件转为字典:如何将字典转为json,将字典写入文件等: 二 josn数据格式简要说明 json对于初学者可以理解是一种数 ...
- Linux 日常操作
Linux 日常操作 */--> Linux 日常操作 Table of Contents 1. 查看硬件信息 1.1. 服务器型号序列号 1.2. 主板型号 1.3. 查看BIOS信息 1.4 ...
- [No000094]SVN学习笔记4-版本库概念与部分日常操作
基本概念 版本库 Subversion 使用集中的数据库,它包含了所有的版本控制文件及其完整历史.这个数据库就是版本库.版本库通常位于运行 Subversion 服务器的文件服务器上,向 Subver ...
- hbase日常操作及维护
一,基本命令: 建表:create 'testtable','coulmn1','coulmn2' 也可以建表时加coulmn的属性如:create 'testtable',{NAME => ' ...
随机推荐
- LC 672. Bulb Switcher II
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- WebView的用法
基本用法 布局文件配置WebView <WebView android:id="@+id/wv_news_detail" android:layout_width=" ...
- Prism框架 如何在主程序中合理的弹出子窗体
说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最常见的实 ...
- java7:(Files.walkFileTree())
1.Files.walkFileTree(): 顺序:顺序访问,碰到目录会一直递归下去 package com.test; import java.io.File; import java.io.IO ...
- java8:(Lambda 表达式,Supplier,@FunctionalInterface,foreach(),Optional,Stream().collect,双冒号,joining,partitioningBy分区,collectingAndThen,filter())
1.Lambda 表达式: 引导:http://www.cnblogs.com/yulinfeng/p/8452379.html DEMO1: List<String> names1 = ...
- Python学习之认知(一)
第二章(一) 2.1 python介绍 2.1.1 python是一种什么样的语言 编程语⾔主要从以下几个⻆度为进行分类,编译型和解释型.静态语言和动态语⾔.强类型定义语言和弱类型定义语言. 编译 ...
- CRLF、CR、LF详解
名词解释 CR:Carriage Return,对应ASCII中转义字符\r,表示回车 LF:Linefeed,对应ASCII中转义字符\n,表示换行 CRLF:Carriage Return &am ...
- 【Linux开发】linux设备驱动归纳总结(二):模块的相关基础概念
linux设备驱动归纳总结(二):模块的相关基础概念 系统平台:Ubuntu 10.04 开发平台:S3C2440开发板 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 基于硬件的消息队列中间件 Solace 简介之二
前言...... 前面简单介绍了Solace来自于哪家公司, 主要能做哪些事情. 本篇主要进一步介绍Solace作为消息传递的中间件如何工作的. 传统意义上来讲, 每当我们谈到消息中间件时, 首先想到 ...
- mac 上iterm终端连接Linux服务后 中文为乱码问题
https://www.jianshu.com/p/8b00f71b2177 编辑 ssh 配置vim /etc/ssh/ssh_config修改如下:Host *#SendEnv LANG LC_* ...