![]() ![]() After issuing the command, psql will open the text editor defined by your EDITOR environment variable and place the most recent command that you entered in psql into the editor.Īfter you type the command in the editor, save it, and close the editor, psql will execute the command and return the result. It is very handy if you can type the command in your favorite editor. dvdrental= # \timingĭvdrental= # Code language: SQL (Structured Query Language) ( sql ) 15) Edit command in your own editor You use the same command \timing to turn it off. To turn on query execution time, you use the \timing command. To get help on specific PostgreSQL statement, you use the \h command.įor example, if you want to know detailed information on ALTER TABLE statement, you use the following command: \h ALTER TABLE Code language: SQL (Structured Query Language) ( sql ) 14) Turn on query execution time ![]() \? Code language: SQL (Structured Query Language) ( sql ) To know all available psql commands, you use the \? command. In case you want to execute psql commands from a file, you use \i command as follows: \i filename Code language: SQL (Structured Query Language) ( sql ) 13) Get help on psql commands If you want to save the command history to a file, you need to specify the file name followed the \s command as follows: \s filename Code language: SQL (Structured Query Language) ( sql ) 12) Execute psql commands from a file \s Code language: SQL (Structured Query Language) ( sql ) To display command history, you use the \s command. Psql executes the previous command again, which is the SELECT statement. Now, you want to save time typing the previous command again, you can use \g command to execute the previous command: \g Code language: SQL (Structured Query Language) ( sql ) To retrieve the current version of PostgreSQL server, you use the version() function as follows: SELECT version() Code language: SQL (Structured Query Language) ( sql ) To list all users and their assign roles, you use \du command: \du Code language: SQL (Structured Query Language) ( sql ) 10) Execute the previous command \dv Code language: SQL (Structured Query Language) ( sql ) 9) List users and their roles To list available views in the current database, you use the \dv command. \df Code language: SQL (Structured Query Language) ( sql ) 8) List available views To list available functions in the current database, you use the \df command. \dn Code language: SQL (Structured Query Language) ( sql ) 7) List available functions To list all schemas of the currently connected database, you use the \dn command. To describe a table such as a column, type, modifiers of columns, etc., you use the following command: \d table_name Code language: SQL (Structured Query Language) ( sql ) 6) List available schema Note that this command shows the only table in the currently connected database. To list all tables in the current database, you use \dt command: \dt Code language: SQL (Structured Query Language) ( sql ) ![]() To list all databases in the current PostgreSQL database server, you use \l command: \l Code language: SQL (Structured Query Language) ( sql ) 4) List available tables You are now connected to database "dvdrental" as user "postgres".ĭvdrental= # Code language: PHP ( php ) 3) List available databases The following command connects to dvdrental database under postgres user: postgres= # \c dvdrental If you omit the user parameter, the current user is assumed. Once you are connected to a database, you can switch the connection to a new database under a user specified by user. In case you want to use SSL mode for the connection, just specify it as shown in the following command: psql -U user -h host "dbname=db sslmode=require" Code language: SQL (Structured Query Language) ( sql ) 2) Switch connection to a new database If you want to connect to a database that resides on another host, you add the -h option as follows: psql -h host -d database -U user -W Code language: SQL (Structured Query Language) ( sql ) psql -d database -U user -WĬode language: SQL (Structured Query Language) ( sql )įor example, to connect to dvdrental database under postgres user, you use the following command: C:\Program Files\PostgreSQL\9.5\bin>psql -d dvdrental -U postgres -Wĭvdrental= # Code language: SQL (Structured Query Language) ( sql ) After pressing Enter PostgreSQL will ask for the password of the user. The following command connects to a database under a specific user. Summary: in this tutorial, we give you a list of common psql commands that help you query data from the PostgreSQL database server faster and more effectively. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |