PostgreSql初探(2)-创建数据库

PostgreSql初探(2)-创建数据库,第1张

概述1.创建数据库 createdb [postgres@gc1 ~] createdb mydb createdb 是一个 SQL 命令 CREATE DATABASE的封装,和在psql里通过create database mydb效果是一样的 具体可以参见文档http://www.postgres.cn/docs/9.3/app-createdb.html 2.访问数据库 psql,就像sqlp

1.创建数据库
createdb
[postgres@gc1 ~] createdb mydb

createdb 是一个 sql 命令 CREATE DATABASE的封装,和在psql里通过create database mydb效果是一样的
具体可以参见文档http://www.postgres.cn/docs/9.3/app-createdb.html

2.访问数据库
psql,就像sqlplus一样
[postgres@gc1 ~] plsq mydb
psql (9.4.4)
Type “help” for help.

mydb=#
最后一行也可能是mydb=>
‘#’意味着你是数据库超级用户

mydb=# select version(); version --------------------------------------------------------------------------------------------------------------- Postgresql 9.4.4 on x86_64-unkNown-linux-gnu,compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48),64-bit(1 row)
mydb=# select version()

不加‘;’的话不会像oracle与MysqL一样等待你输入‘;’

psql程序有一些不属于sql命令的内部命令,他们以‘\’开头

mydb=# helpYou are using psql,the command-line interface to Postgresql.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
mydb-# \hAvailable help:  ABORT                            CREATE FOREIGN DATA WRAPPER DROP SEQUENCE ALTER AGGREGATE CREATE FOREIGN table DROP SERVER ALTER ColLATION CREATE FUNCTION DROP table ALTER CONVERSION CREATE GROUP DROP tableSPACE ALTER DATABASE CREATE INDEX DROP TEXT SEARCH CONfigURATION ALTER DEFAulT PRIVILEGES CREATE LANGUAGE DROP TEXT SEARCH DICTIONARY ALTER DOMAIN CREATE MATERIAliZED VIEW DROP TEXT SEARCH PARSER ALTER EVENT TRIGGER CREATE OPERATOR DROP TEXT SEARCH TEMPLATE ALTER EXTENSION CREATE OPERATOR CLASS DROP TRIGGER ALTER FOREIGN DATA WRAPPER CREATE OPERATOR FAMILY DROP TYPE ALTER FOREIGN table CREATE RolE DROP USER ALTER FUNCTION CREATE RulE DROP USER MAPPing ALTER GROUP CREATE SCHEMA DROP VIEW ALTER INDEX CREATE SEQUENCE END ALTER LANGUAGE CREATE SERVER EXECUTE ALTER LARGE OBJECT CREATE table EXPLAIN ALTER MATERIAliZED VIEW CREATE table AS FETCH ALTER OPERATOR CREATE tableSPACE GRANT --More--

sql命令的帮助信息

mydb=# \?General  \copyright             show Postgresql usage and distribution terms  \g [file] or ;         execute query (and send results to file or |pipe)  \gset [PREFIX]         execute query and store results in psql variables  \h [name]              help on Syntax of sql commands,* for all commands  \q                     quit psql  \watch [SEC]           execute query every SEC secondsquery 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 fileinput/Output  \copy ...              perform sql copY with data stream to the clIEnt host  \echo [STRING]         write string to standard output--More--

psql命令的帮助信息

mydb-# \q
[postgres@gc1 ~]$
\q就是退出了,也不会向MysqL一样说个bye啊:-(

关于plsql的详细内容可以看文档http://www.postgres.cn/docs/9.3/app-psql.html
挑几个有用的说说

testdb=>\set PROMPT1 ‘%n@%m %~%r%# ‘

peter@localhost testdb=>
不用说也能懂了吧

You can display tables in different ways by using the \pset command:

peter@localhost testdb=>\pset border 2border style is 2.peter@localhost testdb=>SELECT * FROM my_table;+-------+--------+| first | second |+-------+--------+|     1 | one ||     2 | two ||     3 | three ||     4 | four |+-------+--------+(4 rows)
peter@localhost testdb=>\pset border 0border style is 0.peter@localhost testdb=>SELECT * FROM my_table;first second----- ------    1 one    2 two    3 three    4 four(4 rows)
peter@localhost testdb=>\pset border 1border style is 1.peter@localhost testdb=>\pset format unalignedOutput format is unaligned.peter@localhost testdb=>\pset fIEldsep ","FIEld separator is ",".peter@localhost testdb=>\pset tuples_onlyShowing only tuples.peter@localhost testdb=>SELECT second,first FROM my_table;one,1two,2three,3four,4

通过\pset改变输出格式

Alternatively,use the short commands:

peter@localhost testdb=>\a \t \xOutput format is aligned.Tuples only is off.Expanded display is on.peter@localhost testdb=>SELECT * FROM my_table;-[ RECORD 1 ]-first  | 1second | one-[ RECORD 2 ]-first  | 2second | two-[ RECORD 3 ]-first  | 3second | three-[ RECORD 4 ]-first  | 4second | four\x  Expanded display
总结

以上是内存溢出为你收集整理的PostgreSql初探(2)-创建数据库全部内容,希望文章能够帮你解决PostgreSql初探(2)-创建数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1175648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存