0
0
PostgreSQLquery~5 mins

psql command-line tool basics in PostgreSQL

Choose your learning style9 modes available
Introduction
psql helps you talk to your PostgreSQL database using simple commands. It lets you run queries and see results right away.
You want to quickly check data in your database without a fancy app.
You need to run SQL commands to create or change tables.
You want to test queries before adding them to your program.
You need to fix or update data directly in the database.
You want to learn SQL by trying commands interactively.
Syntax
PostgreSQL
psql [options] [dbname [username]]
Use 'psql' to start the tool and connect to a database.
You can specify the database name and user to connect as.
Examples
Connects to the database named 'mydatabase' using your current system user.
PostgreSQL
psql mydatabase
Connects to 'mydatabase' as user 'alice'.
PostgreSQL
psql -U alice mydatabase
Connects to 'mydatabase' on the local machine at port 5432.
PostgreSQL
psql -h localhost -p 5432 mydatabase
Sample Program
Shows connection info and the PostgreSQL version.
PostgreSQL
\conninfo
SELECT version();
OutputSuccess
Important Notes
Commands starting with a backslash (\) are psql-specific, not SQL.
Use \q to quit the psql tool.
You can use \? to see a list of all psql commands.
Summary
psql is a simple way to connect and work with PostgreSQL databases.
You start it by typing 'psql' with optional database and user names.
Inside psql, you can run SQL queries and special commands starting with \.