psql -U username -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.

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.

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:

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 ;

Answer from Grant on Stack Overflow
🌐
PostgreSQL
postgresql.org › docs › current › app-psql.html
PostgreSQL: Documentation: 18: psql
May 14, 2026 - Print failed SQL commands to standard error output. This is equivalent to setting the variable ECHO to errors. ... Specifies that psql is to execute the given command string, command. This option can be repeated and combined in any order with the -f option.
🌐
Hasura
hasura.io › blog › top-psql-commands-and-flags-you-need-to-know-postgresql
Top psql commands and flags you need to know | PostgreSQL
You can list all the functions from your database with the \df command. ... The psql interface enables you to list all the database views with the \dv command.
🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › postgresql-psql-commands
PostgreSQL - Psql commands - GeeksforGeeks
July 12, 2025 - The -d option in psql commands is used to state the database name.
🌐
TigerData
tigerdata.com › learn › postgres-cheat-sheet
Postgres Cheat Sheet | Tiger Data
August 26, 2025 - postgres@localhost:~$ psql psql (9.6.12)Type "help" for help.postgres=#Copy · // Enter PostgreSQL Command Line via “psql” client (psql)
🌐
PostgreSQL
tomcam.github.io › postgres
psql command line tutorial and cheat sheet | postgres
This worked to connect to Postgres on DigitalOcean # -U is the username (it will appear in the \l command) # -h is the name of the machine where the server is running. # -p is the port where the database listens to connections. Default is 5432. # -d is the name of the database to connect to. I think DO generated this for me, or maybe PostgreSQL. # Password when asked is csizllepewdypieiib $ psql -U doadmin -h production-sfo-test1-do-user-4866002-0.db.ondigitalocean.com -p 25060 -d mydb # Open a database in a remote location.
🌐
Oracle DBA Training
learnomate.org › home › postgresql › postgresql: basic psql commands
PostgreSQL: Basic psql Commands - Learnomate Technologies
July 20, 2024 - The output proves that the “\i” command successfully executes the commands from a specific file. In psql, the “\timing” command is used to enable or disable query execution time:
Find elsewhere
🌐
GitHub
gist.github.com › basperheim › f76ff098eee7dd44896a4f1f72e393b8
Postgres psql essential commands · GitHub
This DROP TABLE command will perform a cascading "drop" (or deletion) of a table (if it exists): EXECUTE 'DROP TABLE IF EXISTS ' || tbl_name || ' CASCADE'; ... SELECT 'drop table if exists "' || tablename || '" cascade;' FROM pg_tables WHERE schemaname = 'public'; WARNING: The above psql command will drop all tables in the public schema of your PostgreSQL database.
🌐
Snowflake
snowflake.com › en › blog › engineering › get-started-postgres-command-line-psql
Get Started With the Postgres Command Line psql
May 14, 2026 - This is really the bread and butter of psql. ... \c database_name connects to a different database, while \c - username connects to the current database with a different name ... \d shows objects in the database. By itself, \d lists all relations (tables, views and sequences) in the current database. \d table_name describes a table. This is the command you will use the most.
🌐
QuickRef.ME
quickref.me › home › postgresql cheat sheet & quick reference
PostgreSQL Cheat Sheet & Quick Reference
The PostgreSQL cheat sheet provides you with the common PostgreSQL commands and statements. Switch and connect · $ sudo -u postgres psql · List all databases · postgres=# \l · Connect to the database named postgres · postgres=# \c postgres ...
🌐
Bytebase
bytebase.com › references › postgresql how-to guides › top psql commands with examples
Top psql commands with examples | PostgreSQL How-to Guide
The \c command switches to the specified database. This is useful for working with a specific database after you have listed the available databases. postgres-# \c postgres psql (16.0, server 16.1 (Debian 16.1-1.pgdg120+1)) You are now connected to database "postgres" as user "postgres".
🌐
Hacker News
news.ycombinator.com › item
Psql command line tutorial and cheat sheet | Hacker News
November 17, 2020 - Even as someone already comfortable with psql, pgcli is extremely handy · SELECT * FROM parent JOIN child USING (user_id) I personally think naming your primary keys with their full name instead of calling all of them `id` or `key` is better practice also
🌐
Crunchy Data Blog
crunchydata.com › blog › magic-tricks-for-postgres-psql-settings-presets-cho-and-saved-queries
Magic Tricks for Postgres psql: Settings, Presets, Echo, and Saved Queries | Crunchy Data Blog
July 19, 2024 - All of the above things I’ve listed you can set up to happen automatically every time you use your local psql. When psql starts, it looks for a .psqlrc file and if one exists, it will execute the commands within it.
🌐
Censhare
knowledgebase.censhare.com › tk › most-useful-postgresql-commands
Most Useful PostgreSQL Commands | Customer Knowledge Base
3. To retrieve the current version of PostgreSQL database server then use the version() function as follow by connecting psql: ... 4. To see the list of the all available databases then use the following command by connecting psql:
🌐
Neon
neon.com › postgresql › administration › psql-commands
17 Practical psql Commands That You Don't Want To Miss
If you want to save the command history to a file, you need to specify the file name followed the \s command as follows: ... To know all available psql commands, you use the \? command.
🌐
Lick Observatory
ucolick.org › ~de › PG › Man › psql.1.html
PSQL(UNIX) manual page
In normal operation, psql provides a prompt with the name of the database that psql is current connected to followed by the string "=>". For example, Welcome to the POSTGRESQL interactive sql monitor: Please read the file COPYRIGHT for copyright terms of POSTGRESQL · type \? for help on slash commands type \q to quit type \g or terminate with semicolon to execute query You are currently connected to the database: testdb
🌐
Eamonncottrell
blog.eamonncottrell.com › a-list-of-postgresql-commands-for-beginners
A List of PostgreSQL Commands for Beginners
April 8, 2022 - FULL JOIN: This is where some magic happens. The full command is here: SELECT columns FROM table_1 FULL JOIN table_2 ON table_1.primary_key_column = table_2.foreign_key_column;: This hooks up two columns that we previously linked via keys.
🌐
Alessio Ligabue
alessioligabue.it › en › blog › postgresql-complete-guide
PostgreSQL: Complete Guide to Managing Users, Permissions & Databases
July 15, 2025 - A: You can use the psql client with the -U (user) option: psql -U user_name -d database_name. You will be prompted for a password if authentication is not configured via .pgpass or environment variables.
Address   13 Via Ca Marastoni, 42122, Reggio Emilia
🌐
W3Schools
w3schools.com › postgresql › postgresql_getstarted.php
PostgreSQL - Get Started
To insert SQL statements in the SQL Shell command, just write them after the postgres=# command like this: