efoc
efoc

Reputation: 641

How do you run postgreSQL scripts/bash scripts on the command line for a database

I have some shell and java jar files that need to be executed on a database from the command prompt.

Upvotes: 1

Views: 1955

Answers (1)

The Spartan
The Spartan

Reputation: 421

In order to perform SQL queries on a Postgres database, you'll need to log in. The simplest way to execute raw PL/pgSQL (which includes SQL syntax) is by using the psql binary:

psql -U <db_username> -d <db_name> < <raw_PL/pgSQL_file>

If the bash is raw SQL / PL/pgSQL, you can give it directly to the psql command. If it's a script that connects to a database (which it probably is), you'll need to either 1) modify it to connect to your database, or 2) delete everything but the SQL / PL/pgSQL that needs to be run, then pass it to the psql command.

As for the jar, I can't say. It's compiled java code--who knows what database it's trying to connect to (maybe it requires you to pass the database name and the user to log in as?).

EDIT: All of these methods require a database password (if your database has one set up, which I would hope it would).

Upvotes: 2

Related Questions