乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

      PostgreSQL 實現(xiàn)sql放入文件批量執(zhí)行

       2021-04-27 17:06  來源: 腳本之家   我來投稿 撤稿糾錯

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了PostgreSQL 實現(xiàn)sql放入文件批量執(zhí)行,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。

      PostgreSQL sql放入文件,登入數(shù)據(jù)庫之后批量執(zhí)行

      1. 建立測試sql:

      vi aa.sql插入:猜測每條sql語句是用;分隔的,function中的多個;也會自動識別。

      create table tb1(id integer);
      insert into tb1 select generate_series(1,10);
      select * from tb1;
      delete from
      tb1 where id<3;
      select * from tb1;

       

      2. 將aa.sql放入 ./src/postgresql-9.3.5/src/tutorial下(./src/postgresql-9.3.5/src/tutorial是PostgreSQL自動識別的目錄,當(dāng)然也可以放在任意目錄,比如/home/postgres/aa.sql)

      3. 切換用戶登入

      1

      2su postgrespsql postgres

      4. 執(zhí)行:當(dāng)輸入\i時候,會自動檢測到./src/postgresql-9.3.5/src/tutorial下的文件,PostgreSQL的測試?yán)右卜旁诖四夸浵?/p>

      postgres=# \i aa.sql (\i /home/postgres/aa.sql)
       id | name
      ----+------
       1 | join
       2 | join
       3 | join
       4 | join
       5 | join
       6 | join
       7 | join
       8 | join
       9 | join
       10 | join
      (10 rows)
       
      CREATE TABLE
      INSERT 0 10
       id
      ----
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
      (10 rows)
       
      DELETE 2
       id
      ----
       3
       4
       5
       6
       7
       8
       9
       10
      (8 rows)
       
      postgres=#

       

      postgres=# \d tb1
         Table "public.tb1"
       Column | Type  | Modifiers
      --------+---------+-----------
       id   | integer |

       

      第二個例子:

      vi bb.sql:寫入一個function:

      create function func1()returns void as $$
      declare
      begin
      delete from person where id>5;
      delete from tb1 where id>5;
      end
      $$language plpgsql;
       
      select func1();

       

      執(zhí)行前:

      postgres=# select * from person ;
       id | name
      ----+------
       1 | join
       2 | join
       3 | join
       4 | join
       5 | join
       6 | join
       7 | join
       8 | join
       9 | join
       10 | join
      (10 rows)
       
      postgres=# select * from tb1 ;
       id
      ----
       3
       4
       5
       6
       7
       8
       9
       10
      (8 rows)

       

      執(zhí)行:

      postgres=# \i bb.sql
      CREATE FUNCTION
       func1
      -------
       
      (1 row)

       

      執(zhí)行后:

      postgres=# select * from person ;
       id | name
      ----+------
       1 | join
       2 | join
       3 | join
       4 | join
       5 | join
      (5 rows)
       
      postgres=# select * from tb1 ;
       id
      ----
       3
       4
       5
      (3 rows)
       
      postgres=#

       

      5. 也可以使用psql命令執(zhí)行

      1pslq -d postgres -U postgres -f /home/postgres/aa.sql

      補充:PostgreSQL - 用psql 運行SQL文件

      對于預(yù)先寫好的SQL文件,比如/home/user1/updateMyData.sql, 可以有兩種方式來運行這個SQL文件。

      方式一:

      連接db后執(zhí)行SQL文件

      首先通過psql連接到對應(yīng)的db:

      1psql -d db1 -U userA

      接著輸入密碼,進(jìn)入數(shù)據(jù)庫后,輸入:

      1\i /pathA/xxx.sql

      這里有個問題,如果你把SQL文件的路徑里的路徑分隔符寫成了\,會報錯說Permission denied。

      這里的文件路徑必須使用Linux平臺下的路徑分隔符/,否則會報錯。

      方式二:

      直接通過psql命令執(zhí)行SQL文件

      這種方式無需先登錄數(shù)據(jù)庫,直接用一個命令就可以了:

      1psql -d db1 -U userA -f /pathA/xxx.sql

      接著輸入密碼即可執(zhí)行SQL文件到對應(yīng)的db里。

      文章來源:腳本之家

      來源地址:https://www.jb51.net/article/205258.htm

      申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

      相關(guān)文章

      熱門排行

      信息推薦