0

How to add rows into table in the Database by Shell Script? I have following:

Postgres Database Name: dm
Table Name: Error Codes
Password of Database: ******

I want to add rows into table?

I am just starting Shell Scripting...

My code:

#!/bin/sh
DATABASE="dm"
USERNAME="postgres"
HOSTNAME="HBG"
export PGPASSWORD="postgres"

psql -h $HOSTNAME -U $USERNAME $DATABaSE << EOF
select * from Error Codes
EOF

Getting error:

psql: could not connect to server: Connection refused
    Is the server running on host "HBG" (192.168.0.241) and accepting
    TCP/IP connections on port 5432?

1 Answer 1

2

Have a look on how to use the psql command.

You can state any SQL query using this command. For example:

psql -c "SELECT * FROM foo" mydatabase myusername

Of course you can also use INSERT, UPDATE and all other SQL commands, as long as myusername has the rights to do so.

If you want to do this WITHOUT a password (but this is strongly disrecommended and unsecure) you can add this at the start of your script:

set PGPASSWORD=<password>

See here for full documentation: http://www.postgresql.org/docs/7.4/static/app-psql.html

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting thid error:- Is the server running on host "abc" (192.168.1.241) and accepting
What are you trying to tell me?
Plz can you see question again, I update code in it??
HOsTNAME -> linux is case sensitive... You use $HOSTNAME and declare $HOsTNAME. So $HOSTNAME is undefined. Also, is the server actually accepting connections that do not originate from localhost? If not -> Thats a question for ServerFault.
Edited previous comment
|

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.