我在表....中插入了一个数据我想看到整个表的行,列和数据。我如何通过命令显示它?
当前回答
在应用程序(Mac)中打开“SQL Shell (psql)”。
单击enter以获得默认设置。根据提示输入密码。
*)类型\?寻求帮助
*)输入\conninfo查看您连接的用户。
*)输入\l查看数据库列表。
*)通过\c <数据库>的名称连接到数据库,例如\c GeneDB1
您应该看到键提示符更改为新的DB,如下所示:
*)现在您在一个给定的DB中,您想知道该DB的schema。最好的命令是\dn。
其他同样有效(但不是那么好)的命令有select schema_name from information_schema.schemata;从pg_catalog.pg_namespace中选择nspname:
-)现在您已经有了schema,您想知道这些schema中的表。为此,您可以使用dt命令。例如\dt "GeneSchema1".*
*)现在您可以进行查询。例如:
*)下面是上面的DB, Schema和Tables在pgAdmin中的样子:
其他回答
SELECT * FROM my_table;
其中my_table是你的表名。
编辑:
psql -c "SELECT * FROM my_table"
或者只是PSQL,然后键入您的查询。
我还注意到查询
SELECT * FROM tablename;
在PSQL命令提示符上给出错误 而且
SELECT * FROM "tablename";
运行正常,很奇怪,所以别忘了双引号。 我一直喜欢数据库:-(
在应用程序(Mac)中打开“SQL Shell (psql)”。
单击enter以获得默认设置。根据提示输入密码。
*)类型\?寻求帮助
*)输入\conninfo查看您连接的用户。
*)输入\l查看数据库列表。
*)通过\c <数据库>的名称连接到数据库,例如\c GeneDB1
您应该看到键提示符更改为新的DB,如下所示:
*)现在您在一个给定的DB中,您想知道该DB的schema。最好的命令是\dn。
其他同样有效(但不是那么好)的命令有select schema_name from information_schema.schemata;从pg_catalog.pg_namespace中选择nspname:
-)现在您已经有了schema,您想知道这些schema中的表。为此,您可以使用dt命令。例如\dt "GeneSchema1".*
*)现在您可以进行查询。例如:
*)下面是上面的DB, Schema和Tables在pgAdmin中的样子:
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
在有密码保护的数据库中直接执行SQL命令。它宁愿在命令行中使用连接字符串格式。 使用该命令:
psql -d postgresql://postgres:password@localhost:5432/dbname
-c "create database sample1 --or any command"
推荐文章
- 在MongoDB中查找重复的记录
- 对于PostgreSQL表来说,多大才算太大?
- 模式、表和数据库之间的区别是什么?
- 将一列的多个结果行连接为一列,按另一列分组
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- Delete_all vs destroy_all
- 我如何移动一个redis数据库从一个服务器到另一个?
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- 对象'DF__*'依赖于列'*' -将int改为double