我在表....中插入了一个数据我想看到整个表的行,列和数据。我如何通过命令显示它?


当前回答

I have no doubt on @Grant answer. But I face few issues sometimes such as if the column name is similar to any reserved keyword of postgresql such as natural in this case similar SQL is difficult to run from the command line as "\natural\" will be needed in Query field. So my approach is to write the SQL in separate file and run the SQL file from command line. This has another advantage too. If you have to change the query for a large script you do not need to touch the script file or command. Only change the SQL file like this

psql -h localhost -d database -U postgres -p 5432 -a -q -f /path/to/the/file.sql

其他回答

我还注意到查询

SELECT * FROM tablename;

在PSQL命令提示符上给出错误 而且

SELECT * FROM "tablename";

运行正常,很奇怪,所以别忘了双引号。 我一直喜欢数据库:-(

I have no doubt on @Grant answer. But I face few issues sometimes such as if the column name is similar to any reserved keyword of postgresql such as natural in this case similar SQL is difficult to run from the command line as "\natural\" will be needed in Query field. So my approach is to write the SQL in separate file and run the SQL file from command line. This has another advantage too. If you have to change the query for a large script you do not need to touch the script file or command. Only change the SQL file like this

psql -h localhost -d database -U postgres -p 5432 -a -q -f /path/to/the/file.sql

如果您的数据库是密码保护,那么解决方案将是:

PGPASSWORD=password  psql -U username -d dbname -c "select * from my_table"

sql -U username -d mydatabase -c 'SELECT * FROM mytable'

如果你是postgresql的新手,不熟悉使用命令行工具psql,那么当你进入一个交互式会话时,你应该注意一些令人困惑的行为。

例如,发起一个交互式会话:

psql -U username mydatabase 
mydatabase=#

此时,您可以直接输入查询,但必须记住以分号结束查询;

例如:

mydatabase=# SELECT * FROM mytable;

如果您忘记了分号,那么当您按回车键时,您将在返回行中得不到任何东西,因为psql将假设您没有完成输入查询。这会导致各种各样的困惑。例如,如果您重新输入相同的查询,很可能会产生语法错误。

作为一个实验,尝试在psql提示符下输入任何你想要的乱码,然后按enter。PSQL将默默地为您提供一个新行。如果你在新行输入一个分号,然后按回车键,那么你会收到ERROR:

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

经验法则是: 如果你没有收到来自psql的响应,但你期望至少SOMETHING,那么你忘记了分号;

SELECT * FROM my_table;

其中my_table是你的表名。

编辑:

psql -c "SELECT * FROM my_table"

或者只是PSQL,然后键入您的查询。