2013-01-23 33 views
47

我的電腦壞了,幸好我備份了文件夾C:\ Program Files \ PostgreSQL 。如何在PostgreSQL中導出數據庫的模式?

現在我正在新計算機上工作,我想導入存儲在外部磁盤中的以前的 Postgres數據庫。

我想導出位於備份文件夾中的特定數據庫 的模式。

文件PostgreSQL\8.3\data\global\pg_database包含有關數據庫及其OID的信息 ;例如:

"db1" 20012 
"db2" 23456 

我想導出「db1」的模式。

在包含大量文件[500個文件]的文件夾"PostgreSQL\8.3\data\base\20012" 中有一個名爲「20012」的文件夾。

有什麼辦法導出該數據庫的模式?

請注意,所有的Postgresql數據庫文件都位於外部硬盤的 中,我想將該數據庫的模式導出到SQL文件中,獲取該文件,運行它並在本地創建相同的確切數據庫。

回答

81

你應該pg_dump看一看:

pg_dump -s databasename 

將轉儲只架構到stdout爲.SQL。

對於windows,您可能需要撥打pg_dump.exe。我無法訪問Windows機器,但我很確定從內存中獲得這個命令。看看這個幫助是否也適合你。

+0

你確定pg_dump會起作用嗎?導致數據庫的文件位於外部磁盤中,而不是本地文件... – programmer

+0

提供您可以啓動舊的數據庫服務器,是的。您可能已經知道,pg_dump可以遠程連接到數據庫服務器。我試圖在你的新機器上安裝8.3並複製你的舊數據庫文件並啓動postgres,看看是否可行。我以爲你只是在更傾向於更傾向於傾銷模式,我的不好。 – Anew

+1

沒問題,謝謝 – programmer

2

設置一個新的postgresql服務器,並將其數據文件夾替換爲來自外部磁盤的文件。

然後,您就可以啓動PostgreSQL服務器和使用pg_dump的檢索數據(pg_dump的-s的架構只提到)

+0

我用舊的替換了數據文件夾,然後我開始了服務,打開了pgAdmin III應用程序,點擊了服務器圖標[我只有1個服務器,前一個]。我應該創建一個新的服務器嗎?因爲它不起作用...我仍然看到我開始創建的數據庫...而不是備份的數據庫 – programmer

+0

是否確定將文件彈出到正確的數據文件夾中?我沒有在Windows中使用postgresql,所以我不確定數據文件夾的位置。我也不確定,如果pgAdmin緩存任何東西,所以你可能需要重新連接以及... –

+0

以及我只是複製舊的備份數據文件在C:\ Program Files \ PostgreSQL \ 8.3中,我用新的替換它。此外,當我再次打開pgAdmin,它要求輸入舊計算機的密碼,這是一個好的跡象,但事後在數據庫樹中,我沒有看到我的數據庫 – programmer

4

如果你只想要創建表,那麼你可以做pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'

19

的Linux你可以像這樣

pg_dump -U postgres -s postgres > exportFile.dmp 

也許它可以在工作的Windows也 ,如果不嘗試用相同pg_dump.exe

pg_dump.exe -U postgres -s postgres > exportFile.dmp 
+0

在Windows(和Mac)FYI中以相同的方式工作。 –

8

我正在運行的Postgres 9.6,我只好一個特定的模式與數據一起導出。

我用下面的命令:

pg_dump.exe -U username -d databasename -n schemaname > C:\mylocation\mydumpfilename.dmp 

如果只想沒有數據的模式,而不是使用n

下面的開關s是pg_dump的開關列表:

C:\Program Files\PostgreSQL\9.6\bin>pg_dump --help 
pg_dump dumps a database as a text file or to other formats. 

Usage: 
    pg_dump [OPTION]... [DBNAME] 

General options: 
    -f, --file=FILENAME   output file or directory name 
    -F, --format=c|d|t|p   output file format (custom, directory, tar, 
           plain text (default)) 
    -j, --jobs=NUM    use this many parallel jobs to dump 
    -v, --verbose    verbose mode 
    -V, --version    output version information, then exit 
    -Z, --compress=0-9   compression level for compressed formats 
    --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock 
    -?, --help     show this help, then exit 

Options controlling the output content: 
    -a, --data-only    dump only the data, not the schema 
    -b, --blobs     include large objects in dump 
    -c, --clean     clean (drop) database objects before recreating 
    -C, --create     include commands to create database in dump 
    -E, --encoding=ENCODING  dump the data in encoding ENCODING 
    -n, --schema=SCHEMA   dump the named schema(s) only 
    -N, --exclude-schema=SCHEMA do NOT dump the named schema(s) 
    -o, --oids     include OIDs in dump 
    -O, --no-owner    skip restoration of object ownership in 
           plain-text format 
    -s, --schema-only   dump only the schema, no data 
    -S, --superuser=NAME   superuser user name to use in plain-text format 
    -t, --table=TABLE   dump the named table(s) only 
    -T, --exclude-table=TABLE do NOT dump the named table(s) 
    -x, --no-privileges   do not dump privileges (grant/revoke) 
    --binary-upgrade    for use by upgrade utilities only 
    --column-inserts    dump data as INSERT commands with column names 
    --disable-dollar-quoting  disable dollar quoting, use SQL standard quoting 
    --disable-triggers   disable triggers during data-only restore 
    --enable-row-security  enable row security (dump only content user has 
           access to) 
    --exclude-table-data=TABLE do NOT dump data for the named table(s) 
    --if-exists     use IF EXISTS when dropping objects 
    --inserts     dump data as INSERT commands, rather than COPY 
    --no-security-labels   do not dump security label assignments 
    --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs 
    --no-tablespaces    do not dump tablespace assignments 
    --no-unlogged-table-data  do not dump unlogged table data 
    --quote-all-identifiers  quote all identifiers, even if not key words 
    --section=SECTION   dump named section (pre-data, data, or post-data) 
    --serializable-deferrable wait until the dump can run without anomalies 
    --snapshot=SNAPSHOT   use given snapshot for the dump 
    --strict-names    require table and/or schema include patterns to 
           match at least one entity each 
    --use-set-session-authorization 
           use SET SESSION AUTHORIZATION commands instead of 
           ALTER OWNER commands to set ownership 

Connection options: 
    -d, --dbname=DBNAME  database to dump 
    -h, --host=HOSTNAME  database server host or socket directory 
    -p, --port=PORT   database server port number 
    -U, --username=NAME  connect as specified database user 
    -w, --no-password  never prompt for password 
    -W, --password   force password prompt (should happen automatically) 
    --role=ROLENAME   do SET ROLE before dump 

If no database name is supplied, then the PGDATABASE environment 
variable value is used. 

Report bugs to <[email protected]>. 
相關問題