我在表....中插入了一个数据我想看到整个表的行,列和数据。我如何通过命令显示它?
当前回答
SELECT * FROM my_table;
其中my_table是你的表名。
编辑:
psql -c "SELECT * FROM my_table"
或者只是PSQL,然后键入您的查询。
其他回答
在有密码保护的数据库中直接执行SQL命令。它宁愿在命令行中使用连接字符串格式。 使用该命令:
psql -d postgresql://postgres:password@localhost:5432/dbname
-c "create database sample1 --or any command"
在应用程序(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中的样子:
如果您的数据库是密码保护,那么解决方案将是:
PGPASSWORD=password psql -U username -d dbname -c "select * from my_table"
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
打开命令提示符,进入Postgres安装的目录。在我的情况下,我的Postgres路径是“D:\TOOLS\Postgresql-9.4.1-3”。然后移动到Postgres的bin目录。因此命令提示符显示为"D:\TOOLS\Postgresql-9.4.1-3\bin>" 现在我的目标是从用户表中使用“UserId”值选择“UserName”。数据库查询是"Select u "UserName" from users u其中u."UserId"=1"。
对于postgres的psql命令提示符,同样的查询如下所示。
D:\TOOLS\Postgresql-9.4.1-3\bin>psql - u postgress - D DatabaseName -h localhost - t -c "Select u.\"UserName\" from users u其中u.\"UserId\"=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中获得两个整数的最小/最大值?
- 多语言数据库设计的最佳实践是什么?