Postgresql匯入匯出表的例子

來源:互聯網
上載者:User


匯出表

pg_dump -h localhost -U postgres(使用者名稱) 資料庫名(預設時同使用者名稱)  -t table(表名) > dump.sql

匯入表

psql -f dump.sql


完整的匯入匯出表的例子

1,查看一下原資料庫

-bash-3.2$ psql -U playboy -d playboy                                    //原資料庫
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

playboy=> \dt;
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+---------
 public | contents   | table | playboy
 public | entries    | table | playboy
 public | properties | table | playboy
 public | settings   | table | playboy
 public | test       | table | playboy
(5 rows)

playboy=> \q


2,匯出資料庫和表


1.-bash-3.2$ pg_dump -O playboy > /var/lib/pgsql/data/playboy2013.sql      //匯出playboy資料庫  
2. 
3.-bash-3.2$ pg_dumpall > /var/lib/pgsql/data/all_databases2013.sql        //匯出全部資料庫  
4. 
5.-bash-3.2$ pg_dump -O playboy -Ft -t test > /var/lib/pgsql/data/playboy_test2013.tar   //匯出一張表.tar的檔案供pg_restore  
6. 
7.-bash-3.2$ ls /var/lib/pgsql/data |grep 2013                             //查看一下導好了  
8.playboy2013.sql all_databases2013.sql playboy_test2013.tar 
-bash-3.2$ pg_dump -O playboy > /var/lib/pgsql/data/playboy2013.sql      //匯出playboy資料庫

-bash-3.2$ pg_dumpall > /var/lib/pgsql/data/all_databases2013.sql        //匯出全部資料庫

-bash-3.2$ pg_dump -O playboy -Ft -t test > /var/lib/pgsql/data/playboy_test2013.tar   //匯出一張表.tar的檔案供pg_restore

-bash-3.2$ ls /var/lib/pgsql/data |grep 2013                             //查看一下導好了
playboy2013.sql all_databases2013.sql playboy_test2013.tar3,建立新資料庫,並匯入


 
-bash-3.2$ psql -U playboy -d playboy                                    //原資料庫
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

playboy=> \dt;
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+---------
 public | contents   | table | playboy
 public | entries    | table | playboy
 public | properties | table | playboy
 public | settings   | table | playboy
 public | test       | table | playboy
(5 rows)

playboy=> \q

-bash-3.2$ createdb playboy_test -O playboy    //建立一個歸屬playboy的資料庫playboy_test
CREATE DATABASE

-bash-3.2$ pg_restore -d playboy_test /var/lib/pgsql/data/playboy_test2013.tar     //匯入單表,

//將上面匯入表刪除後,在把playboy的資料庫匯入到playboy_test中去,許可權歸屬playboy
-bash-3.2$ psql -d playboy_test -U playboy -f /var/lib/pgsql/data/playboy2013.sql
SET
SET
SET
COMMENT
SET
CREATE SEQUENCE
 setval
--------
     18
(1 row)

SET
SET
CREATE TABLE
CREATE SEQUENCE
 setval
--------
      4
(1 row)

CREATE TABLE
CREATE TABLE
CREATE SEQUENCE
 setval
--------
      3
(1 row)

CREATE TABLE
CREATE TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
CREATE INDEX
REVOKE
REVOKE
GRANT
GRANT

-bash-3.2$ psql -U playboy -d playboy_test         //登入到playboy_test
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

playboy_test=> \dt;                                //查看一下表,根playboy資料庫一樣的。
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | contents   | table | playboy
 public | entries    | table | playboy
 public | properties | table | playboy
 public | settings   | table | playboy
 public | test       | table | playboy

(5 rows)


pgsql匯入寫法比較多,上面已經有二種了,在說一種


1.-bash-3.2$ psql -U playboy playboy_test < /var/lib/pgsql/data/playboy2013.sql 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.