2011年4月7日 星期四

Make Postgresql partition table create in to a function

要建立Postgresql的partition雖然語法不覆雜,但是若要針對不一樣的table,pkey,index來建會就變的無法很自動,
因此可以用下面此function來簡化所有開立的流程(ps.因為目前系統並沒有parent table的check,因此此function需再自行加上
parent table的check



CREATE OR REPLACE FUNCTION dbaegis_gen_partition_table_f(p_table_name varchar,p_part_col varchar, p_part_date varchar)
RETURNS varchar
LANGUAGE plpgsql
AS
$body$
DECLARE
/*
Description: To Generate the partition table
Usage: select dbaegis_gen_partition_table_f(p_table_name,'sql_login_utc','20110320');
to generate partition table for sql_conns on 2011/3/02
use sql to get the result:true/false
Creation: 2011-04-07 By Marc
*/

r1 record;

ls_current_statement VARCHAR(2000) default '';
v_begin_utc int8;
v_end_utc int8;
v_part_date_str varchar(12);
v_table_sql varchar(1024);
v_pkey_sql varchar(1024);
v_index_sql varchar(1024);
v_grant_sql varchar(1024);

BEGIN

ls_current_statement = 'Validate Date';
select extract(epoch from p_part_date::timestamp) into v_begin_utc;
select extract(epoch from p_part_date::timestamp)-86400 into v_end_utc;
select to_char( p_part_date::timestamp,'YYYY_MM_DD') into v_part_date_str;

ls_current_statement = 'Generate Create table sql';

SELECT 'CREATE TABLE ' ||relname|| '_'||v_part_date_str||' ('||
' CHECK( '||p_part_col||' >= '||v_begin_utc||' AND '||p_part_col||' < '||v_end_utc||' )'||
') INHERITS (' ||relname|| ');' AS table_sql
into v_table_sql
FROM pg_class
WHERE relname = p_table_name;

raise notice '%',v_table_sql;
execute v_table_sql;


ls_current_statement = 'Generate Create primary key sql';
--find Primary Key
SELECT 'ALTER TABLE ONLY ' ||ct.relname|| '_'||v_part_date_str||' ADD CONSTRAINT partition_'||v_part_date_str||'_from_' ||ci.relname|| ' PRIMARY KEY (' ||dbaegis_aggregate_concate (a.attname) || ');' AS pkey_sql
into v_pkey_sql
FROM pg_catalog.pg_namespace n,
pg_catalog.pg_class ct,
pg_catalog.pg_class ci,
pg_catalog.pg_attribute a,
pg_catalog.pg_index i
WHERE ct.oid = i.indrelid
AND ci.oid = i.indexrelid
AND a.attrelid = ci.oid
AND i.indisprimary
AND ct.relname = p_table_name
AND ct.relnamespace = n.oid
AND n.nspname = 'public'
GROUP BY ct.relname, ci.relname;

raise notice '%',v_pkey_sql;
execute v_pkey_sql;

ls_current_statement = 'Generate Create index sql';
for r1 in
SELECT 'CREATE ' ||case i.indisunique WHEN TRUE THEN 'unique ' ELSE '' end|| 'INDEX partition_'||v_part_date_str||'_from_' ||ci.relname|| ' ON ' ||ct.relname|| '_'||v_part_date_str||' USING btree (' ||CASE WHEN i.indexprs IS NULL THEN a.attname ELSE pg_get_indexdef (ci.oid,a.attnum,FALSE) END|| ');' AS index_sql
FROM pg_catalog.pg_namespace n,
pg_catalog.pg_class ct,
pg_catalog.pg_class ci,
pg_catalog.pg_attribute a,
pg_catalog.pg_am am,
pg_catalog.pg_index i
WHERE ct.oid = i.indrelid
AND ci.oid = i.indexrelid
AND a.attrelid = ci.oid
AND ci.relam = am.oid
AND n.oid = ct.relnamespace
AND n.nspname = 'public'
AND ct.relname = p_table_name
AND i.indisprimary = FALSE
ORDER BY i.indisunique desc, i.indisclustered, ci.relname loop
v_index_sql = r1.index_sql;

raise notice '%',v_index_sql;
execute v_index_sql;
end loop;

ls_current_statement = 'Generate Create table grant sql';
SELECT 'GRANT ' ||dbaegis_aggregate_concate (privilege_type) || ' ON TABLE ' ||table_name|| '_'||v_part_date_str||' TO webui;' AS grant_sql
into v_grant_sql
FROM information_schema.table_privileges
WHERE table_schema = 'public'
AND grantee = 'webui'
AND table_name = p_table_name
GROUP BY table_schema, table_name;

raise notice '%',v_grant_sql;
execute v_grant_sql;

return p_table_name|| '_'||v_part_date_str;

EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION '<>', ls_current_statement || ' ErrCode: ' || SQLSTATE || '-' || SQLERRM;
return '';
END;
$body$;
/

How to create aggregate varchar with pg function

postgresql 允許使用者自訂Group Function,這是Oracle作不到的,
這裏就一個很實用的aggregate function,把table欄位的字串聯接起來成一筆資料

--先建function
CREATE OR REPLACE FUNCTION dbaegis_concate(text, text)
RETURNS text
LANGUAGE plpgsql
AS
$body$
DECLARE
t text;
BEGIN
IF character_length($1) > 0 AND character_length($2) > 0 THEN
t := $1||','||$2;
ELSIF character_length($1) = 0 AND character_length($2) > 0 THEN
t := ''||$2;
ELSIF character_length($1) > 0 AND character_length($2) = 0 THEN
t := $1;
ELSE t := '';
END IF;

RETURN t;
END;
$body$
VOLATILE
COST 100;
/


--再建aggregate type
CREATE AGGREGATE dbaegis_aggregate_concate(text)
(
sfunc = dbaegis_concate,
stype = text
);
/

2011年3月3日 星期四

重建Database之後,要如何reset postgresql 的serial sequence以符合目前資料的max pkey

1.建立function dbaegis_reset_table_seq_f
2.執行sql: select dbaegis_reset_table_seq_f('customers');

CREATE OR REPLACE FUNCTION dbaegis_reset_table_seq_f(p_table_name varchar)
RETURNS bigint
LANGUAGE plpgsql
AS
$body$
DECLARE

/*
Description: reset the table sequence with current id
Parameter: p_table_name: table name the contain sequence
Return: the max sequence that has been set
Exception: when others return error message
Exampe: select dbaegis_reset_table_seq_f('al_rpt_history');
Creation: 2011-03-03 by Marc Hsiao
*/


ls_current_statement VARCHAR(4096) default '';
v_sql_stmt varchar default '';
r1 record;
r2 record;
v_max_seq bigint default 0;



BEGIN
ls_current_statement = 'Open first cursor';
for r1 in (SELECT 'select (coalesce(max(' ||column_name|| '),0) +1)::bigint as max_seq from ' ||table_name|| ' where ' ||column_name|| ' > 1' as max_seq_sql,
'alter sequence ' ||pg_get_serial_sequence(table_name,column_name) || ' restart with ' AS alter_sql
FROM information_schema.columns
WHERE 1 = 1
and (table_name = p_table_name or p_table_name='')
AND column_default LIKE 'nextval%') loop
ls_current_statement = 'Open second cursor';
for r2 in execute r1.max_seq_sql loop
ls_current_statement = 'assign max_seq';
v_max_seq = r2.max_seq;
ls_current_statement = 'Concate alter statement';
v_sql_stmt = r1.alter_sql||r2.max_seq;
raise notice '%', v_sql_stmt;
ls_current_statement = 'current STMT << '||v_sql_stmt||' >> ';
ls_current_statement = 'Execute alter statement';
execute v_sql_stmt ;
end loop;
end loop;
return v_max_seq;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION '<<dbaegis_reset_table_seq_f error: table:%, failed to execute: % >>', p_table_name, ls_current_statement || ' ErrCode: ' || SQLSTATE || '-' || SQLERRM;
END;
$body$
COST 100
;





2011年3月2日 星期三

PostgreSQL invalid page header in block xxxx of relation base xxxx

有時遇到table 壞軌,要救回資料可以照以下方式來作
reindex db發生error:

database "prod" failed: ERROR: invalid page header in block 6066 of relation base/16384/25370

依照數字可以找出database跟object
select oid,* from pg_database where oid::varchar='16384';
select oid,* from pg_class where oid::varchar='16384';

修復方法:reindex database 或 table
整個DB reindex:
reindexdb -U postgres -a
單一table Reindex:
psql -U postgres -c "reindex table customers"


ps.若是reindex時還是有大量的error,則在postgresql.conf裏最後一行加上zero_damaged_pages = on,並重啟postgresql database


zero_damaged_pages 說明:(8.0以後的postgresql支援)

Detection of a damaged page header normally causes PostgreSQL to report an error, aborting the current command. Setting zero_damaged_pages to true causes the system to instead report a warning, zero out the damaged page, and continue processing. This behavior will destroy data, namely all the rows on the damaged page. But it allows you to get past the error and retriev

2011年2月28日 星期一

use ftp and tar in shell script

ftp -in 192.168.1.2 << _EOF
quote user tester
quote pass tester
cd /download_folder
lcd /upload_folder
put | "tar -cjvf - test.data" test.data.tar.bz2
bye
_EOF

2011年2月22日 星期二

PostgreSQL check DB, Tablespace, Table, Index Size

PostgreSQL有內建的function可以view各種的Size


--check the tablespace size
SELECT spcname,
pg_size_pretty(pg_tablespace_size(spcname)) as size,
pg_tablespace_size(spcname) as for_sort
FROM pg_tablespace
ORDER BY pg_tablespace_size(spcname) DESC;


--check the database size
SELECT datname,
pg_size_pretty(pg_database_size(datname)) as size,
pg_database_size(datname) as for_sort
FROM pg_database
ORDER BY pg_database_size(datname) DESC;


--check the object size
SELECT relname,reltuples,
pg_size_pretty(pg_relation_size(oid)) as size,
pg_relation_size(oid) as for_sort
FROM pg_class
WHERE relkind IN ('r') --public table
ORDER BY pg_relation_size(oid) DESC;



--check table include index size
SELECT relname,reltuples,
pg_size_pretty(pg_total_relation_size(oid)) as size,
pg_total_relation_size(oid) as for_sort
FROM pg_class
WHERE relkind IN ('r') --public table
ORDER BY pg_total_relation_size(oid) DESC;

Postgresql 如何一次清掉所有的view, trigger及function

Postgresql有個麻煩的地方,若view, trigger或function綁著table,就無法在table上作新增欄位的動作,更麻煩的是view綁view時,上一層的view要更新就一定要先drop 下一層的view,不像Oracle這麼方便,因此若是上新版本的Database Schema時,需將view,trigger, function清掉,因此寫了一個shell script來作這個動作

filename: dropDBV.sh

usage ()
{
echo "

$0 version 0.9
put the description here

SYNOPSIS
$0 [ --help ] [ -h PGHOST ] [ -p PGPORT ] [ -U PGUSER ] [-d PGDATABASE]

DESCRITPTION
This script use to clean dbaegis History data
for example:
$0 -d cobra
$0 -h ora2 -p 5432 -U postgres -d cobra


[OPTIONS]
-h PGHOST DATABASE server host name or IP address
-p PGPORT DATABASE server port
-U PGUSER DATABASE USER
-d TARGET_DB DATABASE NAME
-v VERBOSE Purge Data use Verbose Mode

"
exit 0
}



#drop view
sqlDropView="
SELECT 'drop view if exists '||pc.relname||' cascade;'
FROM pg_class pc,
pg_namespace pn
WHERE pc.relkind = 'v'
AND pn.nspname = 'public'
AND pn.oid = pc.relnamespace
order by relname
"

#Trigger
sqlDropTrg="
SELECT 'drop trigger if exists '||tgname||' on '||tbl.relname||' cascade;'
from pg_trigger trg, pg_class tbl, pg_proc src, pg_language l,pg_namespace pn
where trg.tgrelid = tbl.oid
AND l.oid = src.prolang
AND pn.nspname = 'public'
AND pn.oid = tbl.relnamespace
AND trg.tgfoid = src.oid
order by tgname
"


#Drop Function
sqlDropFun="
SELECT 'drop function if exists '||
p.proname||'('||
coalesce(case when pt01.typname::varchar is not null then ''||pt01.typname::varchar end,'')||
coalesce(case when pt02.typname::varchar is not null then ','||pt02.typname::varchar end,'')||
coalesce(case when pt03.typname::varchar is not null then ','||pt03.typname::varchar end,'')||
coalesce(case when pt04.typname::varchar is not null then ','||pt04.typname::varchar end,'')||
coalesce(case when pt05.typname::varchar is not null then ','||pt05.typname::varchar end,'')||
coalesce(case when pt06.typname::varchar is not null then ','||pt06.typname::varchar end,'')||
coalesce(case when pt07.typname::varchar is not null then ','||pt07.typname::varchar end,'')||
coalesce(case when pt08.typname::varchar is not null then ','||pt08.typname::varchar end,'')||
coalesce(case when pt09.typname::varchar is not null then ','||pt09.typname::varchar end,'')||
coalesce(case when pt0a.typname::varchar is not null then ','||pt0a.typname::varchar end,'')||
') cascade;' as args
FROM pg_catalog.PG_PROC p
LEFT JOIN pg_type pt01 on pt01.oid = p.proargtypes[0]
LEFT JOIN pg_type pt02 on pt02.oid = p.proargtypes[1]
LEFT JOIN pg_type pt03 on pt03.oid = p.proargtypes[2]
LEFT JOIN pg_type pt04 on pt04.oid = p.proargtypes[3]
LEFT JOIN pg_type pt05 on pt05.oid = p.proargtypes[4]
LEFT JOIN pg_type pt06 on pt06.oid = p.proargtypes[5]
LEFT JOIN pg_type pt07 on pt07.oid = p.proargtypes[6]
LEFT JOIN pg_type pt08 on pt08.oid = p.proargtypes[7]
LEFT JOIN pg_type pt09 on pt09.oid = p.proargtypes[8]
LEFT JOIN pg_type pt0a on pt0a.oid = p.proargtypes[9]
LEFT JOIN PG_LANGUAGE l ON l.OID = p.prolang ,
pg_namespace pn
WHERE NOT p.proisagg
AND pg_catalog.pg_function_is_visible(p.OID)
AND pn.nspname = 'public'
AND pn.oid = p.pronamespace
-- and p.proname = 'dbaegis_string_apart_id'
AND l.lanname = 'plpgsql'
order by p.proname
"

run_f ()
{
cmdDropView=`psql -t -q ${cmd} -c "${sqlDropView}"`
echo "${cmdDropView}"
psql ${cmd} -c "${cmdDropView}"
cmdDropFun=`psql -t -q ${cmd} -c "${sqlDropFun}"`
psql ${cmd} -c "${cmdDropFun}"
cmdDropTrg=`psql -t -q ${cmd} -c "${sqlDropTrg}"`
psql ${cmd} -c "${cmdDropTrg}"
}

# Start the Program
PGCLIENTENCODING="UTF8"
export PGCLIENTENCODING


while [ ! "$*" = "" ]
do
case $1 in
--help) usage|more;exit 0;;
-h)shift ;PGHOST=$1;;
-p)shift ;PGPORT=$1;;
-U)shift ;PGUSER=$1;;
-v)VERBOSE=Y;;
-d)shift ;PGDATABASE=$1;;
esac
shift
done

if [ x"${PGHOST}" != "x" ] && [ x"${PGPORT}" != "x" ] && [ x"${PGUSER}" != "x" ] && [ x"${PGDATABASE}" != "x" ];
then
cmd=" -h ${PGHOST} -U ${PGUSER} -p ${PGPORT} "
run_f
else
usage
exit 1
fi

2011年2月21日 星期一

Windows command for loop

如何在windows環境下批次修改檔名?
範例一:將所有的檔案加上附檔名apk
for %f in (*.*) do rename %f %f.apk


For

Runs a specified command for each file in a set of files.

Syntax

for {%variable|%%variable} in (set) do command [ CommandLineOptions]

Parameters

{%variable|%%variable} : Required. Represents a replaceable parameter. Use %variable to carry out for from the command prompt. Use %%variable to carry out the forcommand within a batch file. Variables are case-sensitive and must be represented with an alpha value, such as %A, %B, or %C.

(set) : Required. Specifies one or more files, directories, range of values, or text strings that you want to process with the specified command. The parentheses are required.

command : Required. Specifies the command that you want to carry out on each file, directory, range of values, or text string included in the specified (set).

CommandLineOptions : Specifies any command-line options that you want to use with the specified command.

/?: Displays help at the command prompt.

Remarks

Using for

You can use the for command within a batch file or directly from the command prompt.

Using batch parameters

The following attributes apply to the for command:

The for command replaces %variable or %%variable with each text string in the specified set until the command processes all of the files.

For variable names are case-sensitive, global, and no more than 52 total can be active at any one time.

To avoid confusion with the batch parameters %0 through %9, you can use any character for variable except the numerals 0 through 9. For simple batch files, a single character such as %%f works.

You can use multiple values for variable in complex batch files to distinguish different replaceable variables.

Specifying a group of files

The set parameter can represent a single group of files or several groups of files. You can use wildcards (that is, * and ?) to specify a file set. The following are valid file sets:

(*.doc)

(*.doc *.txt *.me)

(jan*.doc jan*.rpt feb*.doc feb*.rpt)

(ar??1991.* ap??1991.*)

When you use the for command, the first value in set replaces %variable or %%variable, and then the specified command processes this value. This continues until all of the files (or groups of files) that correspond to the set value are processed.

Using the in and do keywords

In and do are not parameters, but you must use them with for. If you omit either of these keywords, an error message appears.

Using additional forms of for

If command extensions are enabled (that is, the default), the following additional forms of for are supported:

Directories only

If set contains wildcards (* and ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:

for /D {%% | %}variable in (set) do command [CommandLineOptions]

Recursive

Walks the directory tree rooted at [Drive:]Path, executing the for statement in each directory of the tree. If no directory is specified after /R, the current directory is assumed. If set is just a single period (.), it only enumerates the directory tree. The syntax is:

for /R [[Drive :]Path] {%% | %}variable in (set) do command [CommandLineOptions]

Iterating a range of values

Use an iterative variable to set the starting value (start#) and then step through a set range of values until the value exceeds the set ending value (end#). /L will execute the iterative by comparing start# with end#. If start# is less than end# the command will execute. When the iterative variable exceeds end# the command shell exists the loop. You can also use a negative step# to step through a range in decreasing values. For example, (1,1,5) generates the sequence 1 2 3 4 5 and (5,-1,1) generates the sequence (5 4 3 2 1). The syntax is:

for /L {%% | %}variable in (start#,step#,end#) do command [CommandLineOptions]

Iterating and file parsing

Use file parsing to process command output, strings and file content. Use iterative variables to define the content or strings you want to examine and use the variousParsingKeywords options to further modify the parsing. Use the ParsingKeywords token option to specify which tokens should be passed as iterator variables. Note that when used without the token option, /F will only examine the first token.

File parsing consists of reading the output, string or file content, breaking it up into individual lines of text and then parsing each line into zero or more tokens. The forloop is then called with the iterator variable value set to the token. By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. The different syntaxes are:

for /F ["ParsingKeywords"] {%% | %}variable in (filenameset) do command [CommandLineOptions]

for /F ["ParsingKeywords"] {%% | %}variable in ("LiteralString") do command [CommandLineOptions]

for /F ["ParsingKeywords"] {%% | %}variable in ('command') do command [CommandLineOptions]

The filenameset argument specifies one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. To override the default parsing behavior, specify "ParsingKeywords". This is a quoted string that contains one or more keywords to specify different parsing options.

If you use the usebackq option, use one of the following syntaxes:

for /F ["usebackqParsingKeywords"] {%% | %}variable in ("filenameset") do command [CommandLineOptions]

for /F ["usebackqParsingKeywords"] {%% | %}variable in ('LiteralString') do command [CommandLineOptions]

for /F ["usebackqParsingKeywords"] {%% | %}variable in (`command`) do command [CommandLineOptions]

The following table lists the parsing keywords that you can use for ParsingKeywords.

KeywordDescription

eol=c

Specifies an end of line character (just one character).

skip=n

Specifies the number of lines to skip at the beginning of the file.

delims=xxx

Specifies a delimiter set. This replaces the default delimiter set of space and tab.

tokens=x,y,m-n

Specifies which tokens from each line are to be passed to the for body for each iteration. As a result, additional variable names are allocated. The m-nform is a range, specifying the mth through the nth tokens. If the last character in the tokens= string is an asterisk (*), an additional variable is allocated and receives the remaining text on the line after the last token that is parsed.

usebackq

Specifies that you can use quotation marks to quote file names in filenameset, a back quoted string is executed as a command, and a single quoted string is a literal string command.

Variable substitution

Substitution modifiers for for variable references have been enhanced. The following table lists optional syntax (for any variable I).

Variable with modifierDescription

%~I

Expands %I which removes any surrounding quotation marks ("").

%~fI

Expands %I to a fully qualified path name.

%~dI

Expands %I to a drive letter only.

%~pI

Expands %I to a path only.

%~nI

Expands %I to a file name only.

%~xI

Expands %I to a file extension only.

%~sI

Expands path to contain short names only.

%~aI

Expands %I to the file attributes of file.

%~tI

Expands %I to the date and time of file.

%~zI

Expands %I to the size of file.

%~$PATH:I

Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.

The following table lists modifier combinations that you can use to get compound results.

Variable with combined modifiersDescription

%~dpI

Expands %I to a drive letter and path only.

%~nxI

Expands %I to a file name and extension only.

%~fsI

Expands %I to a full path name with short names only.

%~dp$PATH:I

Searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found.

%~ftzaI

Expands %I to an output line that is like dir.

In the above examples, you can replace %I and PATH by other valid values. A valid for variable name terminates the %~ syntax.

By use uppercase variable names such as %I, you can make your code more readable and avoid confusion with the modifiers, which are not case-sensitive.

Parsing a string

You can use the for /F parsing logic on an immediate string, by wrapping the filenameset between the parentheses in single quotation marks (that is, 'filenameset').Filenameset is treated as a single line of input from a file, and then it is parsed.

Parsing output

You can use the for /F command to parse the output of a command by making the filenameset between the parenthesis a back quoted string. It is treated as a command line, which is passed to a child Cmd.exe and the output is captured into memory and parsed as if it were a file.

Examples

To use for in a batch file, use the following syntax:

for %%variable in (set) do command [CommandLineOptions]

To display the contents of all the files in the current directory that have the extension .doc or .txt using the replaceable variable %f, type:

for %f in (*.doc *.txt) do type %f

In the preceding example, each file that has the .doc or .txt extension in the current directory is substituted for the %f variable until the contents of every file are displayed. To use this command in a batch file, replace every occurrence of %f with %%f. Otherwise, the variable is ignored and an error message is displayed.

To parse a file, ignoring commented lines, type:

for /F "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k

This command parses each line in Myfile.txt, ignoring lines that begin with a semicolon and passing the second and third token from each line to the FOR body (tokens are delimited by commas or spaces). The body of the FOR statement references %i to get the second token, %j to get the third token, and %k to get all of the remaining tokens. If the file names that you supply contain spaces, use quotation marks around the text (for example, "File Name"). To use quotation marks, you must use usebackq. Otherwise, the quotation marks are interpreted as defining a literal string to parse.

%i is explicitly declared in the FOR statement, and %j and %k are implicitly declared by using tokens=. You can specify up to 26 tokens using tokens=, provided that it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'.

To parse the output of a command by placing filenameset between the parentheses, type:

for /F "usebackq delims==" %i IN (`set`) DO @echo %i

This example enumerates the environment variable names in the current environment.

2011年2月20日 星期日

在postgresql 裏如何撈取資料表欄位?

#在postgresql 裏如何撈取資料表欄位

#範例撈取pg_tables的欄位屬性
SELECT a.attname,b.relname,a.attnum
FROM PG_ATTRIBUTE a, PG_CLASS b
WHERE b.oid = a.attrelid
AND b.relname = 'pg_tables'
AND a.attname NOT IN ('tableoid', 'cmax', 'xmax', 'cmin','xmin', 'ctid')
order by attnum;

Postgresql 常用備份及還原

#備份資料Postgresql中的testdb 資料庫到檔按testdb.dmp
pg_dump -F c -i -v -f testdb.dmp testdb

#還原資料庫到Postgresql
#1.建立資料庫testdb2
createdb testdb2
#2.還原資料
pg_restore -F c -i -v -d testedb2 testdb.dmp