亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2021年02月02日 16:11:17   作者:賤一白  
這篇文章主要介紹了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. 切換用戶登入

su postgres
psql 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();

切換到postgres,登入之后執(zhí)行:

執(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í)行

pslq -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:

psql -d db1 -U userA

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

\i /pathA/xxx.sql

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

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

方式二:

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

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

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

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論