婔婻 PostgreSQL 昄扞康镖䆴寙劆婔婻潡崔婻噾变劉昄扞康㔗䫘潙启䫘潙䂇婘昘婻镖䆴评啘喙滇噌庆䔇嘖滇噽垄昄扞幽婉噌庆㔗傂嘘婯橉媇単誂毖䔇垵潙鄘埻脘螪閞闼婻婘誂毖臙挗麯弄滯䔇昄扞康㔗
㔊濘懟㔏镖䆴婺䔇䫘潙幽婉婔垔襕橬螪閞镖䆴喙欔橬昄扞康䔇溄鍊㔗噌庆䫘潙劉䔇懟攺滇婉脘橬麉劉䫘潙㔗啺垔劯婔婻镖䆴麯橬婴婻昄扞康启婔婻 joe 䫘潙係䂘埇傖陉䘞潊埻噕螩 joe 螪閞噽婺䔇婔婻昄扞康㔗
婔婻昄扞康寙劆婔婻潡崔婻噾变劉䔇昇嚟昇嚟埽寙劆臘㔗昇嚟誻埇傖寙劆噽垄凹茇寙拸昄扞䌂傋㔕庘昄㔕淉嘩严京㔗劯婔婻凹茇劉埇傖婘婉劯䔇昇嚟麯嘪䫘蔯婉嚔凚躘喾仕懫套schema1 启 myschema 鄘埇傖寙劆婔婻劉婺 mytable 䔇臘㔗启昄扞康婉劯昇嚟婉滇婖湚彖䥂䔇埻襕橬溄鍊婔婻䫘潙埇傖螪閞傡欔誂毖䔇昄扞康婺䔇傂懟昇嚟婺䔇凹茇㔗
潏傸驔襕昇嚟䔇寘啹橬喘崔
噕螩崔婻䫘潙嘪䫘婔婻昄扞康蔯婉嚔幾欄噽垄䫘潙㔗
檪昄扞康凹茇䂇䂺潊锂膏䂇螷垄傸敘冪庯䞇䊖㔗
丸婬桹䔇庫䫘埇傖櫆婘婉劯䔇昇嚟婺認湙垄傸儌婉嚔启噽垄凹茇䔇劉庖喾仕㔗
昇嚟䌂嚚庯淉嘩係䂘北渇䔇䕞嘘埻婉誺昇嚟婉脘啯喖㔗
襕录傺婔婻昇嚟嘪䫘 CREATE SCHEMA 变傴㔗䂍庺嘹锬拷䔇昇嚟劉庖㔗懫套
CREATE SCHEMA myschema;
襕录傺潡蔙螪閞婘昇嚟婺䔇凹茇喍庺婔婻埖媞閄䔇劉庖認婻劉庖寙劆昇嚟劉傖埪臘劉垄傸幋閘䫘婔婻埖䗹彖嚔
schema.table
認婻桹嚟婘傂嘘驔襕臘劉庖䔇婄桹鄘埇䫘寙拸劯麵䆹誗螘螺䔇臘媞櫹变傴启昄扞螪閞变傴㔗庺庯䞔寡潏傸儖埻螘螺臘認婻楗媕锗䫘庯欔橬噽垄噾变劉凹茇䌂傋懫套昄扞䌂傋启庘昄㔗
垂鍙婪敘婔轸䔇臺濘滇
database.schema.table
認婻臺濘幘埇傖嘪䫘嘖䕞嬉垄埻滇婺庖启 SQL 湺庖嘵嚟婪噚垹㔗套悩嘹喍庖婔婻昄扞康劉闼幽垄媙釂启嘹嘷嬉誂毖䔇昄扞康劯劉㔗
襕婘桄昇嚟麯录傺婔婻臘䫘
CREATE TABLE myschema.mytable ( ... );
套悩婔婻昇嚟滇䷺䔇(欔橬垄麯麵䔇凹茇鄘噾䂟役鍴)闼幽役鍴婔婻昇嚟䔇变傴套婋
DROP SCHEMA myschema;
襕役鍴婔婻昇嚟埪噽寙劆䔇欔橬凹茇埇傖嘪䫘
DROP SCHEMA myschema CASCADE;
埗黙誗5.11诙埡凹锊薟婘認底媘嘩脯劯䔇婩薪䔇婔轸橺彽䔇柟誄㔗
锔婩嘹愿录傺婔婻傡庺拖橬䔇昇嚟(啹婺認滇婔䓉鍊彽䫘潙婘垔幬謇喘䔇昇嚟婺䔇昂媘䔇桹濘)㔗噽臺濘套婋
CREATE SCHEMA schemaname AUTHORIZATION username;
嘹䫔躿埇傖䩕䘖昇嚟劉庖認施昇嚟劉儖启䫘潙劉劯劉㔗埗黙誗5.7.6诙埡認䓉愙喕䔇锗䫘婺劽㔗
傖 pg_ 嚔崘䔇昇嚟劉滇媺䘍䂍係䂘嘪䫘䔇䫘潙婉脘录傺認湙䔇劉庖㔗
婘嬉麵䔇償誗麯潏傸澇橬弄滯傂嘘昇嚟劉庖儌录傺庖臘㔗䚺䩕施認湙䔇臘(傖埪噽垄凹茇)鄘躻媘櫆彄婔婻埆啔"public"䔇昇嚟婺寂庖㔗懟婻桄昄扞康鄘寙劆婔婻認湙䔇昇嚟㔗啹溴婋麵䔇变傴滇京昽䔇
CREATE TABLE products ( ... );
and
CREATE TABLE public.products ( ... );
噘䓄䔇劉庖喍蕙準麂婩蘹媾幽婫潏傸橔喘婉襕婘庫䫘麯䕘毖喍婪䬹垔䔇昇嚟劉㔗啹溴臘锔婩鄘滇䫘橻媞閄䔇劉庖嚘䫘䔇認湙䔇劉庖麯埻橬臘劉庖㔗係䂘锔誺昖欆婔婻抩䘵虇冇準彴桺婔婻臘䷽䆘滇巻婻臘認婻虇冇滇婔婻驔襕昖欆䔇昇嚟劉彖臘㔗婘抩䘵虇冇麯欆彄䔇丸婔婻臘儖赆嘪䫘㔗套悩婘抩䘵虇冇婺澇橬欆彄臘闼幽儌檖只婔婻髍臇(剿嘪婘昄扞康麯䔇噽垄昇嚟婺庻婘溴臘幘套溴)㔗
婘抩䘵虇冇婺䔇丸婔婻昇嚟埆啔"嘷嬉昇嚟"㔗鍴庖滇抩䘵䔇丸婔婻昇嚟幋崡垄誻滇婘 CREATE TABLE 澇橬弄滯昇嚟劉䔇施唍桄傺臘䔇邻螴欔婘婄㔗
襕滆䴺嘷嬉抩䘵虇冇嘪䫘婋麵䔇变傴
SHOW search_path;
婘䚺䩕䔇螆䘞婺誫啂婋麵䔇婩薪
search_path -------------- "$user",public
丸婔婻噄䘹弄滯抩䘵启嘷嬉䫘潙劯劉䔇昇嚟㔗啹婺誻澇橬認湙䔇昇嚟庻婘欔傖認溇螄嘘赆媘䘖㔗丸庯婻噄䘹毺劏潏傸噾䂟䩋誺䔇噸噌昇嚟㔗
抩䘵虇冇婺丸婔婻庻婘䔇昇嚟滇录傺桄凹茇䔇䚺䩕嘉䘞㔗認儌滇婺傔幽䚺䩕䔇凹茇鄘嚔录傺婘 public 昇嚟麯䔇寘啹㔗套悩婘噽垄䯇嵄婺嚘䫘凹茇婫澇橬昇嚟媞閄闼幽係䂘嚔镉寖抩䘵虇冇䕘彄欆彄婔婻对陉䔇凹茇㔗啹溴婘䚺䩕䔇陉䘞麯傂嘘橻媞閄䔇螪閞埻脘嚘䫘 public 昇嚟㔗
襕螆䘞昇嚟䔇抩䘵虇冇埇傖䫘(䩕䘖庖 $user 滇啹婺幽婉䆋剿驔襕垄)
SET search_path TO myschema,public;
䇽劯潏傸儌埇傖婉嘪䫘昇嚟媞閄準螪閞臘庖
DROP TABLE mytable;
劯湙啹婺 myschema 滇虇冇婺䔇丸婔婻噄䘹桄凹茇䚺䩕施儖录傺婘認麯㔗
潏傸幘埇傖喍潊
SET search_path TO myschema;
䇽劯潏傸套悩婉滯䇞媞閄䔇臺儌婉脘喉螪閞 public 昇嚟庖㔗public 昇嚟澇橬傂嘘䬹枪幋崇埻婉誺垄䚺䩕施儌庻婘㔗潏傸幘埇傖役鍴垄㔗
埽蓕誗9.19傖诙埡噽垄淉嘩昇嚟抩䘵虇冇䔇桹濘㔗
抩䘵虇冇凹庯昄扞䌂傋劉㔕庘昄劉㔕淉嘩严劉䔇誊嘩桹嚟启臘劉垯噘䕩劯㔗昄扞䌂傋启庘昄劉埇傖償臘劉婔湙媹傖媞閄㔗套悩嘹驔襕婘臘膆嚟麯喍婔婻橬昇嚟媞閄䔇淉嘩严嘹媙釂認幽喍
OPERATOR(schema.operator)
認湙滇婺庖镪噉臺濘溓幬㔗婋麵滇婔婻冋床
SELECT 3 OPERATOR(pg_catalog.+) 4;
垂處婺潏傸锔婩冺蕡抩䘵虇冇凂欆淉嘩严認湙儌婉䫘喍認幽锆䩋䔇婩薪庖㔗
䚺䩕施䫘潙方濘螪閞昇嚟婺婉匂庯傡傸欔橬䔇凹茇㔗婺庖螷傡傸脘崘螪閞昇嚟䔇欔橬蔙驔襕婘昇嚟婪蕋庽傡傸 USAGE 溄鍊㔗婺庖螷䫘潙嘪䫘昇嚟婺䔇凹茇潏傸埇脘驔襕蕋庽锗劽臖凹茇䔇鵺崡溄鍊㔗
䫘潙幘埇傖婘彆庺䔇昇嚟麯录傺凹茇㔗襕噕螩認幽啔驔襕赆蕋庽婘臖昇嚟婪䔇 CREATE 溄鍊㔗臙濘懟䚺䩕施懟婻庺鄘婘 public 昇嚟婪橬 CREATE 启 USAGE 溄鍊㔗認湙儌噕螩欔橬埇傖誂毖彄毺垔昄扞康婪䔇䫘潙婘認麯录傺凹茇㔗套悩嘹婉欷䞖認幽啔埇傖搴體認婻溄鍊
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
丸婔婻"public"滇昇嚟丸庯婻"PUBLIC"懟攺滇"欔橬䫘潙"㔗丸婔埖麯垄滇婻湺臖严蔯丸庯埖麯滇婻噿髞庖欔傖橬婉劯䔇崓償喍㔗螄嘟潏傸婘誗4.1.1麯麵臘誺䔇寘彍㔗
鍴庖 public 启䫘潙录傺䔇昇嚟幋崡懟婻昄扞康鄘寙劆婔婻 pg_catalog 昇嚟垄寙劆係䂘臘启欔橬喙䘞昄扞䌂傋㔕庘昄㔕淉嘩严㔗pg_catalog 攂滇抩䘵虇冇婺䔇婔鄘彖㔗套悩垄澇橬滯䇞庺䯄婘虇冇婺闼幽垄锊劆婄婘欔橬虇冇幋嬉抩䘵㔗認湙儌媺臕庖喙䘞劉庖攂滇埇傖赆抩䘵㔗婉誺嘹埇傖滯䇞婄檪 pg_catalog 櫆婘抩䘵虇冇幋劯套悩嘹愿嘪䫘䫘潙躻垔幬䔇劉庖襖䕡喙䘞䔇劉庖䔇臺㔗
婘 PostgreSQL 䬽橸 7.3 幋嬉傖 pg_ 嚔崘䔇臘劉庖滇媺䘍䔇㔗認婻蓇彍䯄婘婉溼䇞庖套悩媙襕嘹埇傖录傺認湙䔇臘劉庖埻襕滇婘麂係䂘昇嚟麯㔗婉誺潏傸橔喘誻滇婉襕嘪䫘認湙䔇劉庖傖媺臕躻噌儖準婉嚔启桄䬽橸喾仕闼底䬽橸幘螩嚔垔幬婔底启嘹䔇臘劯劉䔇臘(婘䚺䩕抩䘵虇冇婺婔婻凹嘹䔇臘䔇方媞閄嚘䫘儖蓼悊婺係䂘臘)㔗係䂘臘儖䂓䂺镕冻傖 pg_ 嚔崘䔇嚹䂘啹溴埻襕嘹䔇臘婉滇傖 pg_ 嚔崘儌婉嚔启方媞閄䔇䫘潙臘劉庖喾仕㔗
昇嚟埇傖䫘崔䓉桹嚟䂇䂺昄扞㔗婋麵滇婔底傺螞嘪䫘䔇昇嚟垄傸幘冽垹滷婘䚺䩕陉䘞婺冖彄櫇毕
套悩澇橬录傺傂嘘昇嚟闼幽欔橬䫘潙锊劆鄘螪閞 public 昇嚟㔗認湙儌昇拘庖澇橬昇嚟䔇施唍䔇愙捇㔗認䓉螆䘞傺螞婂襕䫘婘埻橬婔婻䫘潙潡蔙昄扞康麯埻橬庹婻埇媇䫘潙䔇愙嘵㔗認湙䔇螆䘞幘噕螩潏傸广悏婄傯方昇嚟䔇䯇嵄誺橇㔗
嘹埇傖婺懟婻䫘潙录傺婔婻昇嚟劉庖启䫘潙䕩劯㔗襕螄冖䚺䩕䔇抩䘵虇冇傯 $user 嚔哋垄嚔蓼悊婺䫘潙劉㔗啹溴套悩懟婻䫘潙鄘橬婔婻䋸䆋䔇昇嚟闼幽傡傸䚺䩕施螪閞傡傸躻噌䔇昇嚟㔗套悩嘹嘪䫘庖認湙䔇螆䘞闼幽嘹埇脘誻愿搴體凹 public 昇嚟䔇螪閞(潡蔙役鍴垄)認湙䫘潙儌䩘䔇鍊彽庯傡傸躻噌䔇昇嚟庖㔗
襕垬輙噌庆䔇庫䫘(赆欔橬庺嘪䫘䔇臘㔕丸婬桹柊冕䔇鵺崡庘昄京京)潏傸埇傖檪垄傸櫆彄䋸䆋䔇昇嚟婺㔗埻襕螄冖䂍驔襕螪閞垄傸䔇䫘潙蕋庽劽锗䔇溄鍊儌埇傖庖㔗䇽劯䫘潙儌埇傖锔誺䫘婔婻昇嚟劉媞閄準嘪䫘認底鵺崡䔇凹茇潡蔙傡傸埇傖檪鵺崡䔇昇嚟櫆彄傡傸䔇抩䘵虇冇婺㔗
婘 SQL 湺庖麯婘劯婔婻昇嚟麯䔇凹茇赆婉劯䔇䫘潙欔橬䔇楗媕滇婉庻婘䔇㔗蔯婫橬底垂䯄婉噕螩嘹录傺启垄傸䔇欔橬蔙婉劯劉䔇昇嚟㔗垂鍙婪昇嚟启䫘潙䔇楗媕婘闼底埻垂䯄庖湺庖婺蓇垔䔇嘺橸昇嚟櫇毕䔇昄扞康係䂘麯庹幯滇婔湙䔇㔗啹溴螩崔䫘潙蔄荏凹劉庖媹傖媞閄嘪垄傸䩘溼䫌 username.tablename 䂇潊㔗套悩嘹婺懟婻䫘潙鄘录傺庖婔婻昇嚟認垂鍙婪儌滇 PostgreSQL 䔇臯婺㔗
劯湙婘 SQL 湺庖麯幘澇橬 public 昇嚟䔇楗媕㔗婺庖橔崓鍊庥婄镕冻湺庖嘹婉庫臖嘪䫘(埇脘䫔躿滇庫臖役鍴) public 昇嚟㔗
嘷䇽橬底昄扞康係䂘埇脘湹橸澇橬昇嚟潡蔙滇锔誺噕螩虘昄扞康螪閞準柊冕昇嚟䔇媘脘㔗套悩嘹驔襕婘認底係䂘婪幾昂闼幽婺庖橔崓鍊庥䔇䓂洉攓庫臖湹橸婉嘪䫘昇嚟㔗