PostgreSQL guide
Connection Commands
psql
Definition: The PostgreSQL interactive terminal for connecting to a PostgreSQL database.
Usage:
1
psql -h hostname -p port -U username -d database
\connect (or \c)
Definition: Connects to a new database and/or under a different user.
Usage:
1
\\c database_name [username]
\password
Definition: Changes the password for the currently connected user.
Usage:
1
\\password [username]
\conninfo
Definition: Displays information about the current database connection.
Usage:
1
\\conninfo
\encoding
Definition: Shows or sets the client encoding.
Usage:
1
\\encoding [encoding_name]
\quit (or \q)
Definition: Exits the psql program.
Usage:
1
\\q
Database Management
CREATE DATABASE
Definition: Creates a new PostgreSQL database.
Usage:
DROP DATABASE
Definition: Removes a database from the PostgreSQL server.
Usage:
1
DROP DATABASE [IF EXISTS] database_name;
ALTER DATABASE
Definition: Changes the attributes of a database.
Usage:
\list (or \l)
Definition: Lists all databases in the PostgreSQL server.
Usage:
1
\\l
\db+
Definition: Lists all tablespaces with additional information.
Usage:
1
\\db+
CREATE TABLESPACE
Definition: Defines a new tablespace for the database cluster.
Usage:
pg_dump
Definition: Extracts a PostgreSQL database into a script file or other archive file.
Usage:
1
pg_dump -h hostname -p port -U username -F format -f output_file database_name
pg_restore
Definition: Restores a PostgreSQL database from an archive file created by pg_dump.
Usage:
1
pg_restore -h hostname -p port -U username -d database_name archive_file
\copy
Definition: Performs a frontend (client) copy operation to import or export data.
Usage:
VACUUM
Definition: Garbage collection and optional analysis of a database.
Usage:
1
VACUUM [FULL] [FREEZE] [VERBOSE] [ANALYZE] [table_name [(column_name [, ...])]]