Export a Table or Query Result to a CSV File from Postgres

To export data from a table T (or any result of a query) in a PostgreSQL database use the following command:

$ psql -h localhost
       -U database_usr
       -d database_name
       -c "COPY (SELECT * FROM T) 
           TO '/path/file.csv' 
           DELIMITER ',' CSV HEADER;"

Notes:

  • The query should be on one line; it is split here for readability.
  • Make sure you have the correct permissions to write to the directory referenced. The command may need to be run with sudo.

 

Simulate any HTTP Status Code from a URL for Testing

Testing code which calls APIs often involves handling various HTTP status codes from an endpoint.
When calling an external service, it may be difficult to trigger specific HTTP status codes for errors to test out code paths triggered only by those status codes being returned.
We can call the service httpstat.us with the desired response code in the URL path to get any arbitrary HTTP status code returned.

For example, using cURL:

$ curl -v http://httpstat.us/503

The result is:

> GET /503 HTTP/1.1
> Host: httpstat.us
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 503 Service Unavailable
...

Less common success status codes can be simulated as well:

$ curl -v http://httpstat.us/204

Result:

> GET /204 HTTP/1.1
> Host: httpstat.us
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 204 No Content
...

In a config file, we can use httpstat.us URLs instead of our actual dependencies to trigger error cases for tests.

See the full feature list of the HTTP Status service:

http://httpstat.us/