0

I have a shell script which I pass RDS postgres host into, within the script I can connect to postgres db via command below within script. How can I pass psql commands into the shell script to verify a table ? or list all db ? like

\dt 
\l

psql "host=$RDSHOST port=5432 sslmode=disable dbname=mydbname user=testuser password=$PASSWORD"

I get the prompt to DB no issue, would like to pass some commands to verify a db exists or list a table and exit the shell script with its output.

4
  • something similar to $ mysql -h myhost.rds.amazonaws.com -u user -D my_database -p --batch --quick -e "SELECT * FROM my_table" > output.csv Commented Feb 15, 2019 at 9:20
  • was able to get with psql -h $RDSHOST -p 5432 sslmode=disable -d mydbname -U testuser -c 'SELECT * FROM pg_catalog.pg_tables' Commented Feb 15, 2019 at 9:30
  • This works from pgadmin , SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'myschema' \ but I cannot get to run from cli, returns empty Commented Feb 15, 2019 at 13:28
  • I can also list using \d myschema.accounts, cannot seem to read the table though Commented Feb 15, 2019 at 14:31

1 Answer 1

0

From Run PostgreSQL queries from the command line:

psql -U username -d mydatabase -c 'SELECT * FROM mytable'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks John, I can do this ok when logged in, what im trying to do is run the psql command within the shell script after login to aws rds instance or add to existing connestion string , if I try add this command to script it just stops after login to db until I manully exit, if I try add the -c option to the connection string "psql "host=$RDSHOST port=5432 sslmode=disable dbname=mydbname user=testuser password=$PASSWORD" it complains it does not recognize the -c option
John, You pointed me in the right direction thanks , psql -h $RDSHOST -p 5432 sslmode=disable -d mydbname -U testuser -c 'SELECT * FROM pg_catalog.pg_tables'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.