How do I configure heroku psql database using .sh files? - node.js

I have a web app hosted on heroku and I am trying to set up my database by using the same script I have used in offline testing, but on heroku I am not sure how to run my .sh script:
dropdb -U node_user mydb
createdb -U node_user mydb
psql -U node_user mydb < ./bin/sql/firsttable.sql
psql -U node_user mydb < ./bin/sql/secondtable.sql
psql -U node_user mydb < ./bin/sql/thridtable.sql
each of the .sql files include sql commands such as:
CREATE TABLE firsttable(
id SERIAL PRIMARY KEY,
title VARCHAR(64),
user VARCHAR(64),
date TIMESTAMP
);
How do I run my .sh script on heroku's psql so that all of the tables are created?

Run them from your machine using your local psql by connecting to the DB on Heroku.
For that you just need the credentials, which you can obtain with this:
$ heroku pg:credentials:url
Note that you will not be able to drop and create databases on Heroku with dropdb and createdb.
For the SQL files, you can run them as follows:
$ psql $(heroku pg:credentials:url | grep 'postgres://') -f local_file.sql

Related

POSTGRES (psql) - getting ERROR: must be owner of database, but I am doing it with the owner of the database

I have a script which deploys a web app to production. Currently, I am trying to implement a script in the begining which will "empty" the test database, before the database migrations are applied on it. This will be done to prevent "invalid/dirty database" problems, as everything will be newly created before attempting the migrations. Just a note, this runs in a centOS Docker container.
Anyways, this is what I tried to do:
psql -U MYUSER \
-h ${postgres_host} -c "DROP DATABASE my_test_database;"
psql -U MYUSER \
-h ${postgres_host} -c "CREATE DATABASE my_test_database;"
psql -U MYUSER -h ${postgres_host} -d \
my_test_database -c "CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA
public;"
Running the script gave me the following error:
psql: FATAL: Peer authentication failed for user MYUSER
Doing a little bit of research, I found out about peer authentication, and found a way around it (without having to edit the pg_conf file). The way was to pass the password as well.
So, for the three commands I did the following:
PGPASSWORD=${postgres_password} psql -U MYUSERNAME
-h ${postgres_host} -c "DROP DATABASE test_database;"
PGPASSWORD=${postgres_password} psql -U MYUSERNAME
-h ${postgres_host} -c "CREATE DATABASE test_database;"
PGPASSWORD=${postgres_password} psql -U MYUSERNAME -h ${postgres_host} -d \
test_database -c "CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA
public;"
The parameters I pass are defined earlier in the script. But now, I get the following error:
ERROR: must be owner of database test_database
However, the user I specify is THE owner. Can anyone point me toward the right way to do it or give me advice? If you need more info, I will be happy to oblige. Thank you in advance!

Passing psql commands within shell script after connecting to AWS RDS postgres

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.
From Run PostgreSQL queries from the command line:
psql -U username -d mydatabase -c 'SELECT * FROM mytable'

postgres: invalid argument: "psql"

When I perform sudo -u zorgan postgres psql to start my postgres session it returns:
postgres: invalid argument: "psql"
if I remove psql it returns:
postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
Any idea what the problem is?
https://www.sudo.ws/man/1.8.18/sudo.man.html
[-u user] [command]
so in your case:
sudo -u zorgan postgres psql
tries to start postgres process with argument psql and thankfully fails.
if you want to connect to postgres cluster using psql client, use psql. so if you wish to run it with sudo:
sudo -u zorgan psql postgres
here postgres is argumentfor psql, which is dbname...
https://www.postgresql.org/docs/current/static/app-psql.html
psql [option...] [dbname [username]]
sudo -u postgres psql
The postgres above is a user.

What is the difference between sudo -u postgres psql and sudo psql -U postgres?

I'm new to Postgres and Bash so I'm not sure what the difference is.
I'm trying to automate in a bash script updating a table in Postgres. I have the .sql file and I've created .pgpass file with 600.
The a script that is provided to me uses sudo -u postgres psql db -w < .sql and it fails because it can't find the pass.
Whereas, sudo psql -U postgres db -w < .sql doesn't prompt for a pass and is able to update.
So what's the difference? Why can't the first command get the pass from the .pgpass?
sudo -u postgres is running the rest of the command string as the UNIX user postgres
sudo psql -U postgres db -w is running the command as the UNIX user root and (presumeably) connecting to postgres as the user "postgres"
Probably the .pgpass file doesn't exist for the unix user postgres.
It is a case of peer autentication. If you're running user x and you have user x on your database you're trusted by postgres so you don't have to use password (default settings of instalation). Running sudo psql -u x you're trying to connect from user root to database as user x... root!=x so you need password. Client authentication is controlled by a configuration file pg_hba.conf You can also provide password via .pgpass file. You'll find all needed informations in PostgreSQL documentation.

Scripting automated postgres setup

I'm scripting a system setup and already have postgres installed. Here is a test script (run as root) to try and report the working directory in postgres. Calling pwd as postgres gives /var/lib/postgresql. But the test..
#!/bin/bash
su - postgres
pwd > /home/me/postgres_report
exit
.. fails (obviously) and reports the original working directory. And afterwards the bash shell is stuck in postgres, suggesting the commands are not being called in right order. I understand the bash environmental issues here. I don't have a clue how to do what I need to do, which is automate a postgres process that I can easily do interactively (i.e. step into postgres, execute a command, and exit). Any pointers?
Use sudo.
Use one of:
Passing a one line command to psql
sudo -u postgres psql -c "SELECT ..."`
A here document:
sudo -u postgres psql <<"__END__"
SELECT ...;
SELECT ...;
__END__
(If you want to be able to substitute in shell variables leave out the ", e.g. <<__END__, and backslash escape $ signs you don't want to be variables)
Pass a file to psql
sudo -u postgres psql -f /path/to/file
The sudo -u postgres is of course only required if you need to become the postgres system user to run tasks as the postgres database user via peer authentication. Otherwise you can use psql -U username, a .pgpass file, etc.
#!/bin/bash
# run as root
[ "$USER" = "root" ] || exec sudo "$0" "$#"
echo "=== $BASH_SOURCE on $(hostname -f) at $(date)" >&2
sudo passwd postgres
echo start the postgres
sudo /etc/init.d/postgresql start
sudo su - postgres -c \
"psql <<__END__
SELECT 'crate the same user' ;
CREATE USER $USER ;
ALTER USER $USER CREATEDB;
SELECT 'grant him the priviledges' ;
grant all privileges on database postgres to $USER ;
alter user postgres password 'secret';
SELECT 'AND VERIFY' ;
select * from information_schema.role_table_grants
where grantee='""$USER""' ;
SELECT 'INSTALL EXTENSIONS' ;
CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";
CREATE EXTENSION IF NOT EXISTS \"pgcrypto\";
CREATE EXTENSION IF NOT EXISTS \"dblink\";
__END__
"
sudo /etc/init.d/postgresql status
sudo netstat -tulntp | grep -i postgres

Resources