我在表....中插入了一个数据我想看到整个表的行,列和数据。我如何通过命令显示它?
当前回答
## 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;"
其他回答
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";
运行正常,很奇怪,所以别忘了双引号。 我一直喜欢数据库:-(
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;"
推荐文章
- 比较两个SQL Server数据库(模式和数据)的最佳工具是什么?
- PostgreSQL删除所有内容
- 为什么PostgreSQL要对索引列进行顺序扫描?
- PostgreSQL INSERT ON冲突更新(upsert)使用所有排除的值
- 如何检查一个表是否存在于给定的模式中
- 如何在SQL Server Management Studio中查看查询历史
- 如何将整数转换为字符串作为PostgreSQL查询的一部分?
- Psycopg2:用一个查询插入多行
- PostgreSQL返回JSON数组的结果集?
- PostgreSQL通配符LIKE用于单词列表中的任何一个
- 如何创建数据库的MongoDB转储?
- 检查Postgres JSON数组是否包含字符串
- psql: FATAL: Peer authentication failed for user "dev"
- 如何在Postgres/SQL中获得两个整数的最小/最大值?
- 多语言数据库设计的最佳实践是什么?