从命令行运行postgresql查询

时间:2023-01-06 22:45:24

I inserted a data into a table....I wanna see now whole table with rows and columns and data. How I can display it through command?

我将一个数据插入一个表....现在我想要看到整个表格有行,列和数据。如何通过命令显示它?

3 个解决方案

#1


300  

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

psql -U用户名-d mydatabase -c 'SELECT * FROM mytable'

If you're new to postgresql and unfamiliar with using the command line tool psql then there is some confusing behaviour you should be aware of when you've entered an interactive session.

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

For example, initiate an interactive session:

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

psql -U username mydatabase 
mydatabase=#

At this point you can enter a query directly but you must remember to terminate the query with a semicolon ;

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

For example:

例如:

mydatabase=# SELECT * FROM mytable;

If you forget the semicolon then when you hit enter you will get nothing on your return line because psql will be assuming that you have not finished entering your query. This can lead to all kinds of confusion. For example, if you re-enter the same query you will have most likely create a syntax error.

如果您忘记了分号,那么当您点击enter时,您将在返回行上一无所获,因为psql将假定您还没有输入查询。这会导致各种混乱。例如,如果重新输入相同的查询,很可能会产生语法错误。

As an experiment, try typing any garble you want at the psql prompt then hit enter. psql will silently provide you with a new line. If you enter a semicolon on that new line and then hit enter, then you will receive the ERROR:

作为实验,尝试在psql提示符下输入任何你想要的garble,然后点击回车。psql将默默地为您提供新的行。如果您在新行上输入分号,然后单击enter,您将收到错误:

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

The rule of thumb is: If you received no response from psql but you were expecting at least SOMETHING, then you forgot the semicolon ;

经验法则是:如果您没有收到来自psql的响应,但您至少期望得到一些东西,那么您就忘记了分号;

#2


47  

SELECT * FROM my_table;

where my_table is the name of your table.

其中my_table是表的名称。

EDIT:

编辑:

psql -c "SELECT * FROM my_table"

or just psql and then type your queries.

或者只输入psql,然后输入查询。

#3


5  

If your DB is password protected, then the solution would be:

如果您的DB是密码保护的,那么解决方案是:

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

#1


300  

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

psql -U用户名-d mydatabase -c 'SELECT * FROM mytable'

If you're new to postgresql and unfamiliar with using the command line tool psql then there is some confusing behaviour you should be aware of when you've entered an interactive session.

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

For example, initiate an interactive session:

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

psql -U username mydatabase 
mydatabase=#

At this point you can enter a query directly but you must remember to terminate the query with a semicolon ;

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

For example:

例如:

mydatabase=# SELECT * FROM mytable;

If you forget the semicolon then when you hit enter you will get nothing on your return line because psql will be assuming that you have not finished entering your query. This can lead to all kinds of confusion. For example, if you re-enter the same query you will have most likely create a syntax error.

如果您忘记了分号,那么当您点击enter时,您将在返回行上一无所获,因为psql将假定您还没有输入查询。这会导致各种混乱。例如,如果重新输入相同的查询,很可能会产生语法错误。

As an experiment, try typing any garble you want at the psql prompt then hit enter. psql will silently provide you with a new line. If you enter a semicolon on that new line and then hit enter, then you will receive the ERROR:

作为实验,尝试在psql提示符下输入任何你想要的garble,然后点击回车。psql将默默地为您提供新的行。如果您在新行上输入分号,然后单击enter,您将收到错误:

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

The rule of thumb is: If you received no response from psql but you were expecting at least SOMETHING, then you forgot the semicolon ;

经验法则是:如果您没有收到来自psql的响应,但您至少期望得到一些东西,那么您就忘记了分号;

#2


47  

SELECT * FROM my_table;

where my_table is the name of your table.

其中my_table是表的名称。

EDIT:

编辑:

psql -c "SELECT * FROM my_table"

or just psql and then type your queries.

或者只输入psql,然后输入查询。

#3


5  

If your DB is password protected, then the solution would be:

如果您的DB是密码保护的,那么解决方案是:

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