The cursor moves to the next line, which proves that the “\o” command executes successfully. Execute the “\o” command followed by the “file name”, as shown below: \o 'C:/exeFile.txt' The SQL Shell supports a “\o” command that allows us to save query results to a specific file. The output shows that the “\s” command successfully retrieves the query history.Įxample 13: Execute psql Commands From a Particular File Open the terminal, log into “psql”, and execute the following command to see the query history: \s The “\du” command returns the “role name”, “attributes”, and “members details”. To get the user’s list, use the below command: \du In PostgreSQL, the users can have database privileges and can own the database objects, such as tables, schemas, etc. ![]() The “\df” command returns the “schema name”, “function name”, “result data types”, and “argument data types”. To obtain the list of available functions, use the “\df”: \df In Postgres, the functions enhance code reusability, understandability, debugging, etc. The “\dv” command returns the view name, schema name, relation type, and view’s owner. To show the list of views, use the “\dv” command: \dv ![]() Views are a frequently utilized concept in Postgres that allows us to simplify complex queries. The given command returns the names of all schemas along with their owners. To fetch the list of schemas, use the “\dn” command: \dn The stated command retrieves all the details regarding the “emp_data” table, such as column names, column types, columns’ default values, etc.Ī Postgres schema is a namespace that keeps the database objects, such as relations, functions, etc. The above-stated command will describe the “emp_data” table: The “\d” command successfully retrieves the “schema name”, “table name”, “relation type”, and owner.Įxecute the “\d” command followed by the table name to describe a specific table in Postgres: \d emp_data ![]() Postgres users can use the “\d” command to get the list of relations, including sequences, views, etc. The stated command returns all the tables available in the selected database. Run the “\dt” command from SQL Shell to fetch the list of available tables/relations: \dt In Postgres, the tables are used to represent the data elements in a well-organized format. The connection with the “sample_db” database has been established successfully. To accomplish this task, execute the “\c” command from the “SQL Shell”: \c sample_db Performing any operation on a database object requires accessing that database. The “\l” successfully retrieves the list of available databases.Įxample 4: Accessing/Switching a Database Listing available databases is a very common task in Postgres that can be accomplished via the “\l” command: \l The output shows that currently “PostgreSQL 15.1” is running on our system. The output signifies that a connection with the “postgres” database has been successfully established under the user “postgres”.Įxecuting the “SELECT VERSION() ” command will retrieve the currently installed Postgres version: SELECT VERSION() Open the CMD and execute the below-provided psql command to establish a connection to a particular database: psql -d postgres -U postgres Let’s put these commands into practice to get a profound understanding.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |