{"id":13623463,"url":"https://github.com/fphilipe/psql2csv","last_synced_at":"2025-10-20T05:10:46.169Z","repository":{"id":1878298,"uuid":"41854441","full_name":"fphilipe/psql2csv","owner":"fphilipe","description":"Run a query in psql and output the result as CSV.","archived":false,"fork":false,"pushed_at":"2022-02-23T17:40:42.000Z","size":29,"stargazers_count":184,"open_issues_count":2,"forks_count":23,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-09T18:17:40.544Z","etag":null,"topics":["cli","csv","homebrew","homebrew-formula","postgres","postgresql","psql"],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fphilipe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-03T10:39:08.000Z","updated_at":"2024-11-13T14:55:51.000Z","dependencies_parsed_at":"2022-08-06T11:15:26.742Z","dependency_job_id":null,"html_url":"https://github.com/fphilipe/psql2csv","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fphilipe%2Fpsql2csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fphilipe%2Fpsql2csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fphilipe%2Fpsql2csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fphilipe%2Fpsql2csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fphilipe","download_url":"https://codeload.github.com/fphilipe/psql2csv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085321,"owners_count":21045139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","csv","homebrew","homebrew-formula","postgres","postgresql","psql"],"created_at":"2024-08-01T21:01:32.068Z","updated_at":"2025-10-20T05:10:46.082Z","avatar_url":"https://github.com/fphilipe.png","language":"Shell","readme":"# psql2csv\n\n**Run a query in psql and output the result as CSV.**\n\n## Installation\n\n### Mac OS X\n\npsql2csv is available on [Homebrew](http://brew.sh/).\n\n    $ brew install psql2csv\n\n### Manual\n\nGrab the file `psql2csv`, put in somewhere in your `$PATH`, and make it\nexecutable:\n\n    $ curl https://raw.githubusercontent.com/fphilipe/psql2csv/master/psql2csv \u003e /usr/local/bin/psql2csv \u0026\u0026 chmod +x /usr/local/bin/psql2csv\n\n## Usage\n\n    psql2csv [OPTIONS] \u003c QUERY\n    psql2csv [OPTIONS] QUERY\n\n## Options\n\nThe query is assumed to be the contents of STDIN, if present, or the last\nargument. All other arguments are forwarded to psql except for these:\n\n    -?, --help                 show this help, then exit\n    --delimiter=DELIMITER      set the field delimiter (default: ,)\n    --quote=QUOTE              set the quote delimiter (default: \")\n    --escape=ESCAPE            set the escape character (default: QUOTE)\n    --null=NULL                set the string representing NULL; printed without quotes (default: empty string)\n    --force-quote=FORCE_QUOTE  set the columns to be force quoted; comma separated list of columns or * for all (default: none)\n    --encoding=ENCODING        set the output encoding; Excel likes latin1 (default: UTF8)\n    --no-header                do not output a header\n    --timezone=TIMEZONE        set the timezone config before running the query\n    --search-path=SEARCH_PATH  set the search_path config before running the query\n    --dry-run                  print the COPY statement that would be run without actually running it\n\n## Example Usage\n\n    $ psql2csv dbname \"select * from table\" \u003e data.csv\n\n    $ psql2csv dbname \u003c query.sql \u003e data.csv\n\n    $ psql2csv --no-header --delimiter=$'\\t' --encoding=latin1 dbname \u003c\u003csql\n    \u003e SELECT *\n    \u003e FROM some_table\n    \u003e WHERE some_condition\n    \u003e LIMIT 10\n    \u003e sql\n\n## Advanced Usage\n\nLet's assume you have a script `monthly_report.sql` that you run every month.\nThis script has a `WHERE` that limits the report to a certain month:\n\n    WHERE date_trunc('month', created_at) = '2019-01-01'\n\nEvery time you run it you have to edit the script to change the month you want\nto run it for. Wouldn't it be nice to be able to specify a variable instead?\n\nTurns out psql does have support for [variables] which you can pass to psql (and\nthus to psql2csv) via `-v`, `--variable`, or `--set`. To interpolate the\nvariable into the query you can use `:VAR` for the literal value, `:'VAR'` for\nthe value as a string, or `:\"VAR\"` for the value as an identifier.\n\nLet's change the `WHERE` clause in `monthly_report.sql` file to use a variable\ninstead:\n\n    WHERE date_trunc('month', created_at) = (:'MONTH' || '-01')::timestamptz\n\nWith this change we can now run the query for any desired month as follows:\n\n    $ psql2csv -v MONTH=2019-02 \u003c monthly_report.sql \u003e data.csv\n\n[variables]: https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES\n\n## Further Help\n\n- [PostgreSQL COPY documentation](http://www.postgresql.org/docs/current/static/sql-copy.html)\n- [psql variables](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES)\n\n## Author\n\nPhilipe Fatio ([@fphilipe](https://github.com/fphilipe))\n","funding_links":[],"categories":["Shell","Uncategorized","Compiled list","CLI"],"sub_categories":["Uncategorized","plv8:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffphilipe%2Fpsql2csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffphilipe%2Fpsql2csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffphilipe%2Fpsql2csv/lists"}