PostgreSQL: execute command remotely

Your database setup would look something like this. 

create database <database-name>;
create user <user-name> with password <user-postgresql-password>;

For the rest of this post I am assuming you run Linux.  

 

Option #1: remote database server

If you only want to connect

PGPASSWORD=<user-postgresql-password> psql -h <ip/url-of-the-remote-server> -U <user-name> -d <database-name>

If you want to execute a command remotely

PGPASSWORD=<user-postgresql-password> psql -h <ip/url-of-the-remote-server> -U <user-name> -d <database-name> -c "YOUR SQL COMMAND;"

If your SQL commands are in a .sql file and you want to execute that file on the remote server

PGPASSWORD=<user-postgresql-password> psql -h <ip/url-of-the-remote-server> -U <user-name> -d <database-name> < /path/to/your/local/file.sql

Option #2: local database server

If your database on the same machine you will be running the command, just replace <ip/url-of-the-remote-server> by localhost.