psql is a terminal-based frontend to PostgreSQL that enables you to type in queries interactively, issue them to Postgres, and see the query results.

This page shows you how to use the psql command line tool to interact with your Timescale Cloud service.

Before integrating:

On many operating systems, psql is installed by default. To use the functionality described in this page, best practice is to use the latest version of psql. To check the version running on your system:

If you already have the latest version of psql installed, proceed to the Connect to your service section.

If there is no existing installation, take the following steps to install psql:

To use psql to connect to your service, you need the connection details. See Find your connection details.

Connect to your service with either:

  • The parameter flags:

    psql -h <HOSTNAME> -p <PORT> -U <USERNAME> -W -d <DATABASENAME>
  • The service URL:

    psql "postgres://<USERNAME>@<HOSTNAME>:<PORT>/<DATABASENAME>?sslmode=require"

    You are prompted to provide the password.

  • The service URL with the password already included and a stricter SSL mode enabled:

    psql "postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORT>/<DATABASENAME>?sslmode=verify-full"

When you start using psql, these are the commands you are likely to use most frequently:

CommandDescription
\c <DB_NAME>Connect to a new database
\d <TABLE_NAME>Show the details of a table
\dfList functions in the current database
\df+List all functions with more details
\diList all indexes from all tables
\dnList all schemas in the current database
\dtList available tables
\duList PostgreSQL database roles
\dvList views in current schema
\dv+List all views with more details
\dxShow all installed extensions
ef <FUNCTION_NAME>Edit a function
\hShow help on syntax of SQL commands
\lList available databases
\password <USERNAME>Change the password for the user
\qQuit psql
\setShow system variables list
\timingShow how long a query took to execute
\xShow expanded query results
\?List all psql slash commands

For more on psql commands, see the Timescale psql cheat sheet and psql documentation.

When you run queries in psql, the results are shown in the console by default. If you are running queries that have a lot of results, you might like to save the results into a comma-separated .csv file instead. You can do this using the COPY command. For example:

\copy (SELECT * FROM ...) TO '/tmp/output.csv' (format CSV);

This command sends the results of the query to a new file called output.csv in the /tmp/ directory. You can open the file using any spreadsheet program.

To run multi-line queries in psql, use the EOF delimiter. For example:

psql -d $TARGET -f -v hypertable=<hypertable> - <<'EOF'
SELECT public.alter_job(j.id, scheduled=>true)
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.hypertable h ON h.id = j.hypertable_id
WHERE j.proc_schema IN ('_timescaledb_internal', '_timescaledb_functions')
AND j.proc_name = 'policy_compression'
AND j.id >= 1000
AND format('%I.%I', h.schema_name, h.table_name)::text::regclass = :'hypertable'::text::regclass;
EOF

Sometimes, queries can get very long, and you might make a mistake when you try typing it the first time around. If you have made a mistake in a long query, instead of retyping it, you can use a built-in text editor, which is based on Vim. Launch the query editor with the \e command. Your previous query is loaded into the editor. When you have made your changes, press Esc, then type :wq to save the changes, and return to the command prompt. Access the edited query by pressing , and press Enter to run it.

Keywords

Found an issue on this page?Report an issue or Edit this page in GitHub.