现在的位置: 首页 > 综合 > 正文

[postgresql]postgresql操作记录

2014年09月05日 ⁄ 综合 ⁄ 共 1061字 ⁄ 字号 评论关闭
文章目录

1、查找一个数据库下所有table

一个database下的table可以分成三个部分,一部分是用户自己创建的,一般都会在模式的public下面,剩下两个部分则是pg_catalog和information_schema,就在目录下面。

如果要查找所有的table,可以使用select * from pg_tables查看

     schemaname     |        tablename        | tableowner | tablespace | hasindexes | hasrules | hastriggers
--------------------+-------------------------+------------+------------+------------+----------+-------------
 public             | author_base             | jincheng   |            | f          | f        | f
 information_schema | sql_implementation_info | postgres   |            | f          | f        | f
 pg_catalog         | pg_attribute            | postgres   |            | t          | f        | f
 pg_catalog         | pg_authid               | postgres   | pg_global  | t          | f        | f

如果只需要查找用户自己创建的table,则只需要使用select * from pg_tables where tableowner = 'jincheng'

2、导入导出表数据

使用copy命令将表数据导入到txt文件或从txt文件导入数据

copy tablename to 'filepath' with delimiter as '|';
copy tablename from 'filepath' with delimiter as '|';

使用pg_dump转储数据库

转储一个数据库∶
pg_dump mydb > db.out
重载这个数据库∶
psql -d database -f db.out
输出一个叫 mydb 的包含BLOB 的数据库到一个 tar 文件:
pg_dump -Ft -b mydb > db.tar
把这个数据库(连同BLOB) 一起恢复到一个现有的叫 newdb 的数据库:
pg_restore -d newdb db.tar

导出单张表
pg_dump -t tablename dbname > db.out

附pg_dump参数一个PostgreSQL 7.3 文档-pg_dump

抱歉!评论已关闭.