从命令行运行PostgreSQL查询 [英] Run PostgreSQL queries from the command line

查看:194
本文介绍了从命令行运行PostgreSQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据插入到表中。...我现在想看整个表,其中包括行,列和数据。如何通过命令显示它?

I inserted a data into a table....I wanna see now whole table with rows and columns and data. How I can display it through command?

推荐答案

psql -U用户名-d mydatabase -c'SELECT * FROM mytable'

如果您是Postgresql的新手,并且不熟悉使用命令行工具 psql ,进入交互式会话时,您应该意识到一些令人困惑的行为。

If you're new to postgresql and unfamiliar with using the command line tool psql then there is some confusing behaviour you should be aware of when you've entered an interactive session.

例如,启动一个交互式会话:

For example, initiate an interactive session:

psql -U username mydatabase 
mydatabase=#

这时您可以直接输入查询,但是您必须记住以分号;

At this point you can enter a query directly but you must remember to terminate the query with a semicolon ;

例如:

mydatabase=# SELECT * FROM mytable;

如果您忘记了分号,那么当您按下Enter键时,返回行将什么也不会得到,因为 psql 将假定您尚未完成输入查询。这可能导致各种混乱。例如,如果重新输入相同的查询,很可能会产生语法错误。

If you forget the semicolon then when you hit enter you will get nothing on your return line because psql will be assuming that you have not finished entering your query. This can lead to all kinds of confusion. For example, if you re-enter the same query you will have most likely create a syntax error.

作为实验,尝试在psql提示符下键入任何所需的乱码然后按回车。 psql 会以静默方式为您提供新的一行。如果您在该新行上输入分号,然后按Enter,则将收到错误:

As an experiment, try typing any garble you want at the psql prompt then hit enter. psql will silently provide you with a new line. If you enter a semicolon on that new line and then hit enter, then you will receive the ERROR:

mydatabase=# asdfs 
mydatabase=# ;  
ERROR:  syntax error at or near "asdfs"
LINE 1: asdfs
    ^

经验法则是:
如果您没有收到 psql 的任何答复,但您期望至少有所作为,则您忘记了分号;

The rule of thumb is: If you received no response from psql but you were expecting at least SOMETHING, then you forgot the semicolon ;

这篇关于从命令行运行PostgreSQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆