Postgresql 在后台运行的是一个叫做 postmaster 的程序,缺省的 TCP/IP 端口为 5432。 在客户端可以有很多工具连接到服务器上操作数据,对于一个 Unix 下的系统管理员而言,熟练掌握 psql 环境下的数据操作是十分需要的。 在启动 psql 之前,当然要求 psql 在你的 PATH 路径上,所以我们需要指定: $ export PATH=$PATH:/usr/local/pgsql/bin 然后我们就可以进入了: [user@host user]$ psql testdb Welcome to psql,the Postgresql interactive terminal. Type: \copyright for distribution terms \h for help with sql commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit testdb=# 记住在 psql 后面必须跟上数据库名字,不像 MysqL ,可以用 use 命令来切换数据库。 所有的 psql 相关的命令都必须以 \ 开头,如果不知道某个命令的用法,可以用 \? 来找到帮助。 \g 为执行查询语句 \q 为退出 psql 环境 \h 为帮助 booktown=# \? \a toggle between unaligned and aligned mode \c[onnect] [dbname|- [user]] connect to new database (currently 'booktown') \C <title> table title \copy ... perform sql copY with data stream to the client machine \copyright show Postgresql usage and distribution terms \d <table> describe table (or view,index,sequence) \d{t|i|s|v} list tables/indices/sequences/views \d{p|S|l} list permissions/system tables/lobjects \da list aggregates \dd [object] list comment for table,type,function,or operator \df list functions \do list operators \dT list data types \e [file] edit the current query buffer or [file] with external editor \echo <text> write text to stdout \encoding <encoding> set client encoding \f <sep> change field separator \g [file] send query to backend (and results in [file] or |pipe) \h [cmd] help on Syntax of sql commands,* for all commands \H toggle HTML mode (currently off) \i <file> read and execute queries from <file> \l list all databases \lo_export,\lo_import,\lo_list,\lo_unlink large object operations \o [file] send all query results to [file],or |pipe \p show the content of the current query buffer \pset <opt> set table output <opt> = {format|border|expanded|fieldsep| null|recordsep|tuples_only|title|tableattr|pager} \q quit psql \qecho <text> write text to query output stream (see \o) \r reset (clear) the query buffer \s [file] print history or save it in [file] \set <var> <value> set internal variable \t show only rows (currently off) \T <tags> HTML table tags \unset <var> unset (delete) internal variable \w <file> write current query buffer to a <file> \x toggle expanded output (currently off) \z list table access permissions \! [cmd] shell escape or command 因此,综合上面的说明,我们可以知道: \dt 命令是显示表 \di 显示索引 \ds 显示序列 \dv 显示视图 \dp 显示权限 \dS 显示系统表 \dl 显示 lobjects \d tablename 显示表的结构/索引/序列 \d indexname 显示索引的详细信息 执行过的查询语句可以用 \s \i 分别存盘和调入。 psql 中 sql 语句的输入和 MysqL 类似,一直到分号才结束,但是不同的是,在分号结束前的换行,在 MysqL 里面是 > 符号,而在 psql 里是 -# 符号。 缓冲区的管理 在编辑查询语句缓冲之前,最好设定好你最熟悉的编辑器,例如 vi 的话: $ set EDITOR='vi' $ export EDITOR \e 命令就可以进入编辑,用上你熟悉的 vi 指令来编辑了。 值得一提的是 Postgresql 具有子查询和视图等概念,而 MysqL 到现在还没有。 子查询的例子: booktown=# SELECT title FROM books booktown-# WHERE author_id = (SELECT id FROM authors booktown(# WHERE last_name='Geisel' booktown(# AND first_name='Theodor Seuss'); 视图的例子: CREATE VIEW recent_shipments booktown-# AS SELECT count(*) AS num_shipped,max(ship_date),title booktown-# FROM shipments booktown-# JOIN editions USING (isbn) booktown-# NATURAL JOIN books AS b (book_id) booktown-# GROUP BY b.title booktown-# ORDER BY num_shipped DESC; 从普通的 sql 语句而言,应该说 Postgresql 和 MysqL 是很相似的。因此,Postgresql 在一个企业级的应用中,应该比 MysqL 更加具有优势。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。