title: How to run a bash script with psql
body: <p>Sorry for the extremely noob questions but I'm completely new to shell scripting.</p>
  <p>Simply put, I need to connect to a PostgreSQL database hosted on AWS, obtain some data, save it as a CSV and turn this process into an executable bash script. I have succeeded in doing this on pgAdmin but am unable to do so from my terminal.</p>
  <p>Here is what I'm currently doing:</p>
  <pre><code>#!/bin/bash
  
  psql 
  -U username
  -h hostname
  -d database
  -p password 
  
  copy 
  (SELECT data FROM table LIMIT 100;) to &quot;Users/name/Downloads&quot; With CSV
  </code></pre>
  <p>Now I can't seem to complete the process of connecting to the database within the bash script, and there may also be a problem with the SQL Query as it acts differently with psql than on pgAdmin.</p>
  <p>According to what I could find it may be useful to use su or sudo commands, some people are doing --U instead of -U but nothing i've tried seems to work thus far.</p>
  <p>For reference, I am using PostgreSQL version 12. I'm new to the game and could use a little help, where am I going wrong here?</p>