Joseph
Joseph

Reputation: 128

Add rows into Database table using Shell Script?

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?

Upvotes: 0

Views: 3589

Answers (1)

Nidhoegger
Nidhoegger

Reputation: 5252

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

Upvotes: 2

Related Questions