我在表....中插入了一个数据我想看到整个表的行,列和数据。我如何通过命令显示它?
当前回答
## Using SQL file
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db -a -q -f /path/to/sql/file.sql
## Or
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db < /path/to/sql/file.sql
## Using command
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db -c "select * from my table limit 1;"
其他回答
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,那么你忘记了分号;
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 my_table;
其中my_table是你的表名。
编辑:
psql -c "SELECT * FROM my_table"
或者只是PSQL,然后键入您的查询。
我将添加我的经验,一个命令,在windows机器上。 我想尝试运行一个命令,从中我将获得表内容。
这是一个对我有用的命令:
psql -U postgres -d typeorm -c "SELECT * FROM \"Author\";
- u postgres - user -d typeorm -我要连接的数据库 - c…-我的查询命令 ; ——分号
我遇到了一些问题,主要是弄清楚如何准确地设置查询部分。我尝试了不同的命令,比如:用',",(),但除了这个符号外,没有什么对我有效。
## Using SQL file
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db -a -q -f /path/to/sql/file.sql
## Or
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db < /path/to/sql/file.sql
## Using command
PGPASSWORD=myPassword psql -h myDbHost -U myUser -p 5432 -d my_db -c "select * from my table limit 1;"
推荐文章
- 在MongoDB中查找重复的记录
- 对于PostgreSQL表来说,多大才算太大?
- 模式、表和数据库之间的区别是什么?
- 将一列的多个结果行连接为一列,按另一列分组
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- Delete_all vs destroy_all
- 我如何移动一个redis数据库从一个服务器到另一个?
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- 对象'DF__*'依赖于列'*' -将int改为double