在postgresql中通過命令行執(zhí)行sql文件
通過命令行執(zhí)行初始化sql腳本是比較常見的需求,命令行下執(zhí)行如下操作即可:
若是執(zhí)行的命名只是創(chuàng)建用戶,編輯用戶,創(chuàng)建數(shù)據(jù)庫(kù)的話可以不指定-d參數(shù)。
psql -U username -d myDataBase -a -f init.sql
如果是遠(yuǎn)程數(shù)據(jù)庫(kù)加入-h參數(shù)指定主機(jī)地址即可
psql -h host -U username -d myDataBase -a -f init.sql
補(bǔ)充:PostgreSQL操作-psql基本命令
一、建立數(shù)據(jù)庫(kù)連接
接入PostgreSQL數(shù)據(jù)庫(kù): psql -h IP地址 -p 端口 -U 數(shù)據(jù)庫(kù)名
之后會(huì)要求輸入數(shù)據(jù)庫(kù)密碼
二、訪問數(shù)據(jù)庫(kù)
1、列舉數(shù)據(jù)庫(kù):\l
2、選擇數(shù)據(jù)庫(kù):\c 數(shù)據(jù)庫(kù)名
3、查看該某個(gè)庫(kù)中的所有表:\dt
4、切換數(shù)據(jù)庫(kù):\c interface
5、查看某個(gè)庫(kù)中的某個(gè)表結(jié)構(gòu):\d 表名
6、查看某個(gè)庫(kù)中某個(gè)表的記錄:select * from apps limit 1;
7、顯示字符集:\encoding
8、退出psgl:\q
列出當(dāng)前數(shù)據(jù)庫(kù)所有表
\dt列出表名
SELECT tablename FROM pg_tables; WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' ORDER BY tablename;
列出數(shù)據(jù)庫(kù)名
\l或
SELECT datname FROM pg_database;
切換數(shù)據(jù)庫(kù)
\c 數(shù)據(jù)庫(kù)名
1、通過命令行查詢
\d 數(shù)據(jù)庫(kù) —— 得到所有表的名字
\d 表名 —— 得到表結(jié)構(gòu)
2、通過SQL語句查詢
"select * from pg_tables"
—— 得到當(dāng)前db中所有表的信息(這里pg_tables是系統(tǒng)視圖)
"select tablename from pg_tables where schemaname='public'"
—— 得到所有用戶自定義表的名字(這里"tablename"字段是表的名字,"schemaname"是schema的名字。用戶自定義的表,如果未經(jīng)特殊處理,默認(rèn)都是放在名為public的schema下)
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S] [PATTERN] list conversions
\dC [PATTERN] list casts
\dd[S] [PATTERN] show comments on objects
\ddp [PATTERN] list default privileges
\dD[S] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\l[+] list all databases
\sf[+] FUNCNAME show a function's definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset NAME [VALUE] set table output option
(NAME := {format|border|expanded|fieldsep|footer|null|
numericlocale|recordsep|tuples_only|title|tableattr|pager})
\t [on|off] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off] toggle expanded output (currently off)
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "postgres")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection
Operating System
\cd [DIR] change the current working directory
\timing [on|off] toggle timing of commands (currently off)
\! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
postgresql數(shù)據(jù)管理系統(tǒng)使用命令方式有兩種:
1. 內(nèi)部命令,以反斜線開始 \ ,如: \l 顯示所有數(shù)據(jù)庫(kù)
2. 標(biāo)準(zhǔn)SQL命令,以分號(hào) ; 或 \g 結(jié)束,可以使用多行
數(shù)據(jù)庫(kù)的關(guān)鍵操作:
1. 啟動(dòng)服務(wù) 2. 登錄 3. 建立數(shù)據(jù)庫(kù) 4. 建立表 5. 插入記錄到表中
6. 更新/刪除/查詢/修改操作 7. 退出 8. 停止服務(wù)
在windows7中安裝的postgresql默認(rèn)使用GBK字符集,經(jīng)常不能使用顯示中文的數(shù)據(jù)表,解決辦法:
注意:在windows 7下的postgresql中寫操作時(shí)要使用GBK,讀操作時(shí)要用UTF8;
設(shè)置字符集為 utf-8 就可以了.
postgres=# \encoding utf-8 // 設(shè)置客戶端的字元集 postgres=# \encoding // 顯示客戶端的字元集 postgres=# show client_encoding; // 顯示客戶端的字元集 postgres=# show server_encoding; // 顯示服務(wù)器的字元集
啟動(dòng)服務(wù):
net start postgresql-9.5
停止服務(wù):
net stop postgresql-9.5
獲取命令幫助:
c:\> psql --help
登錄( 注意: postgres 是默認(rèn)用戶即管理員 ):
路徑 psql -h 服務(wù)器 -U 用戶名 -d 數(shù)據(jù)庫(kù) -p 端口地址 // -U 是大寫 C:\> psql -h localhost -U postgres -p 5432 // 默認(rèn)打開postgres數(shù)據(jù)庫(kù) C:\> psql -h 127.0.0.1 -U postgres -d fengdos -p 5432 // 打開fengdos數(shù)據(jù)庫(kù) C:\> psql -U postgres // 快速登錄(全部使用默認(rèn)設(shè)置) // 使用某些有密碼的用戶的情況下, 會(huì)提示輸入密碼. 用戶 postgres 的口令: ILoveYou // 輸入時(shí)不會(huì)顯示任何字符 // 成功后顯示: psql (9.5.3) 輸入 "help" 來獲取幫助信息. // 進(jìn)入postgresql數(shù)據(jù)庫(kù)系統(tǒng)提示符狀態(tài), ******=# 中=#前面為當(dāng)前使用的數(shù)據(jù)庫(kù) postgres=# help // 獲取系統(tǒng)幫助,顯示如下:
您正在使用psql, 這是一種用于訪問PostgreSQL的命令行界面
鍵入:
\copyright 顯示發(fā)行條款
\h 顯示 SQL 命令的說明
\? 顯示 pgsql 命令的說明 (pgsql內(nèi)部命令)
\g 或者以分號(hào)(;)結(jié)尾以執(zhí)行查詢
\q 退出注: 數(shù)據(jù)庫(kù)名稱區(qū)分大小寫的。
postgres=# \help // 獲取SQL命令的幫助,同 \h postgres=# \quit // 退出,同 \q postgres=# \password dlf // 重新設(shè)置用戶dlf的密碼,然后需要 \q退出后才生效 c:\>psql exampledb < user.sql // 將user.sql文件導(dǎo)入到exampled數(shù)據(jù)庫(kù)中 postgres=# \h select // 精細(xì)顯示SQL命令中的select命令的使用方法 postgres=# \l // 顯示所有數(shù)據(jù)庫(kù) postgres=# \dt // 顯示當(dāng)前數(shù)據(jù)庫(kù)中的所有表 postgres=# \d [table_name] // 顯示當(dāng)前數(shù)據(jù)庫(kù)的指定表的表結(jié)構(gòu) postgres=# \c [database_name] // 切換到指定數(shù)據(jù)庫(kù),相當(dāng)于use postgres=# \du // 顯示所有用戶 postgres=# \conninfo // 顯示當(dāng)前數(shù)據(jù)庫(kù)和連接信息 postgres=# \e // 進(jìn)入記事本sql腳本編輯狀態(tài)(輸入批命令后關(guān)閉將自動(dòng)在命令行中執(zhí)行) postgres=# \di // 查看索引(要建立關(guān)聯(lián)) postgres=# \prompt [文本] 名稱 // 提示用戶設(shè)定內(nèi)部變數(shù) postgres=# \encoding [字元編碼名稱] // 顯示或設(shè)定用戶端字元編碼 *可以將存儲(chǔ)過程寫在文本文件中aaa.sql,然后在psql狀態(tài)下: postgres=# \i aaa.sql // 將aaa.sql導(dǎo)入(到當(dāng)前數(shù)據(jù)庫(kù)) postgres=# \df // 查看所有存儲(chǔ)過程(函數(shù)) postgres=# \df+ name // 查看某一存儲(chǔ)過程 postgres=# select version(); // 獲取版本信息 postgres=# select usename from pg_user; // 獲取系統(tǒng)用戶信息 postgres=# drop User 用戶名 // 刪除用戶
其它SQL命令通用如(標(biāo)準(zhǔn)化SQL語句):
*創(chuàng)建數(shù)據(jù)庫(kù):
create database [數(shù)據(jù)庫(kù)名];
*刪除數(shù)據(jù)庫(kù):
drop database [數(shù)據(jù)庫(kù)名];
*創(chuàng)建表:
create table ([字段名1] [類型1] ;,[字段名2] [類型2],......<,primary key (字段名m,字段名n,...)>;);
*在表中插入數(shù)據(jù):
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
*顯示表內(nèi)容:
select * from student;
*重命名一個(gè)表:
alter table [表名A] rename to [表名B];
*刪除一個(gè)表:
drop table [表名];
*在已有的表里添加字段:
alter table [表名] add column [字段名] [類型];
*刪除表中的字段:
alter table [表名] drop column [字段名];
*重命名一個(gè)字段:
alter table [表名] rename column [字段名A] to [字段名B];
*給一個(gè)字段設(shè)置缺省值:
alter table [表名] alter column [字段名] set default [新的默認(rèn)值];
*去除缺省值:
alter table [表名] alter column [字段名] drop default;
*修改表中的某行某列的數(shù)據(jù):
update [表名] set [目標(biāo)字段名]=[目標(biāo)值] where [該行特征];
*刪除表中某行數(shù)據(jù):
delete from [表名] where [該行特征]; delete from [表名]; // 刪空整個(gè)表
*可以使用pg_dump和pg_dumpall來完成。比如備份sales數(shù)據(jù)庫(kù):
pg_dump drupal>/opt/Postgresql/backup/1.bak
1.列出所有表名的查詢語句
SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' ORDER BY tablename;
2.列出表中所有的數(shù)據(jù)
SELECT * FROM someTable;
3.執(zhí)行外部腳本
#/opt/PostgreSQL/8. 3/bin/psql - Upostgres ;登陸到數(shù)據(jù)庫(kù)的控制臺(tái)界面 postgres= # \i /root/db. sql \i 命令用于執(zhí)行一個(gè)外部的sql腳本文件。
4.導(dǎo)出數(shù)據(jù)庫(kù)為外部的腳本
#/opt/PostgreSQL/8. 3/bin/ pg_dump - Upostgres - C - fdb. sql database -C create -f 是導(dǎo)出后的文件名
5.postgresql 插入16進(jìn)制數(shù)
INSERT INTO tableAAA VALUES( x'0001f' : : integer, '鑒權(quán)' , 'Authority' )
6.使用 TG_RELNAME 報(bào)錯(cuò)ERROR: syntax error at or near "$1" at character
[引]http://www.dbmonster.com/Uwe/Forum.aspx/postgresql/2051/TG-RELNAME-problem Perhaps you will get some idea if you read the document: 37. 6. 4. Executing Dynamic Commands
改:執(zhí)行動(dòng)態(tài)語句
EXECUTE 'INSERT INTO TG_RELNAME VALUES (NEW.start_time , NEW.id , NEW.end_time)';
7. psql 常用命令
a. \c tesdb1 - - 將當(dāng)前連接的testdb數(shù)據(jù)庫(kù)改變成 testdb1 。
b . \q - - 斷開與Postgres服務(wù)器的連接
c . \l 列出所有數(shù)據(jù)庫(kù)的名字
\l+ 列出所有數(shù)據(jù)庫(kù)的名字以及字符集編碼
d. \d [ 名字] 描述表, 索引, 序列, 或者視圖
列出表/索引/序列/視圖/系統(tǒng)表
\d{t| i| s| v| S} [ 模式] ( 加 "+" 獲取更多信息)
- - 列出表/索引/序列/視圖/系統(tǒng)表
\d tablename - - 查看表的結(jié)構(gòu)
\dt - - 列出數(shù)據(jù)庫(kù)中所有表
8.在PostgreSQL中如何刪除重復(fù)記錄
在PostgreSQL中刪除重復(fù)記錄其實(shí)很簡(jiǎn)單,不論有多少行重復(fù),只要在要?jiǎng)h除重復(fù)記錄的表中table加一列rownum字段( id為table表中的主鍵) ,類型設(shè)置為serial類型即可,然后執(zhí)行sql
delete from deltest where rownum not in( select max(rownum) from deltest );
最后刪除列rownum即可
正文:
連接數(shù)據(jù)庫(kù)操作:
psql是postgresql數(shù)據(jù)庫(kù)提供的連接數(shù)據(jù)庫(kù)shell命令,格式 psql 【option】 dbname
在終端輸入psql 會(huì)使用默認(rèn)的方式連接本地?cái)?shù)據(jù)庫(kù),使用的用戶名是登陸linux系統(tǒng)使用的用戶名,
psql -U username -W pass 以及psql -U username -W pass databasenaem都可以實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的功能,第一種方式是使用用戶名username密碼pass連接默認(rèn)數(shù)據(jù)庫(kù)(具體鏈接那個(gè)數(shù)據(jù)庫(kù)還沒搞清 楚),第二種方式使用用戶名username密碼pass連接username數(shù)據(jù)庫(kù)。如果登錄成功之后將顯示類似信息
Welcome to psql 8.0.6, 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
連接成功之后所有的命令都是使用”\“+ 字符或者word完成相應(yīng)的功能?,F(xiàn)將常用的幾個(gè)列車
\l 列出所有數(shù)據(jù)庫(kù)
\dt 列出連接數(shù)據(jù)庫(kù)中所有表
\di 列出連接數(shù)據(jù)庫(kù)中所有index
\dv 列出連接數(shù)據(jù)庫(kù)中所有view
\h sql命令幫助
\? \ 所有命令幫助
\q 退出連接
\d tablename 列出指定tablename的表結(jié)構(gòu)
可以嘗試執(zhí)行下面兩句sql
SELECT current_date SELECT version()
是不是nothing happened,這是因?yàn)閜ostgresql數(shù)據(jù)庫(kù)要求必須使用;結(jié)尾否則不予執(zhí)行,加上;之后就能看到結(jié)果了。
如果我們想創(chuàng)建數(shù)據(jù)庫(kù)怎么辦呢?
我們知道createdb和dropdb可以創(chuàng)建和刪除數(shù)據(jù)庫(kù),但是如果我們這個(gè)時(shí)候執(zhí)行出現(xiàn)什么問題呢?可以試一試,提示是個(gè)錯(cuò)誤。
為什么呢?
createdb和dropdb是shell腳本,所以現(xiàn)在又兩種方式執(zhí)行
(1).退出連接進(jìn)入終端,輸入createdb test —U user -W pass 稍等提示創(chuàng)建數(shù)據(jù)庫(kù)成功
dropdb test —U user -W pass 提示drop成功
(2).在未退出連接中使用 \! createdb test —U user -W pass 稍等提示創(chuàng)建數(shù)據(jù)庫(kù)成功
\! dropdb test —U user -W pass 提示drop成功
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL 序列綁定字段與不綁定字段的區(qū)別說明
這篇文章主要介紹了PostgreSQL 序列綁定字段與不綁定字段的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
postgresql 實(shí)現(xiàn)獲取所有表名,字段名,字段類型,注釋
這篇文章主要介紹了postgresql 實(shí)現(xiàn)獲取所有表名,字段名,字段類型,注釋操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PGSQL 實(shí)現(xiàn)查詢今天,昨天的數(shù)據(jù),一個(gè)月之內(nèi)的數(shù)據(jù)
這篇文章主要介紹了PGSQL 實(shí)現(xiàn)查詢今天,昨天的數(shù)據(jù),一個(gè)月之內(nèi)的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
postgresql 將逗號(hào)分隔的字符串轉(zhuǎn)為多行的實(shí)例
這篇文章主要介紹了postgresql 將逗號(hào)分隔的字符串轉(zhuǎn)為多行的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
基于postgresql查詢某個(gè)字段屬于哪個(gè)表的說明
這篇文章主要介紹了基于postgresql查詢某個(gè)字段屬于哪個(gè)表的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL拼接字符串的幾種方法簡(jiǎn)單示例
在PostgreSQL中有多種方式可以拼接字符串,這篇文章主要給大家介紹了關(guān)于PostgreSQL拼接字符串的幾種方法,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

