Run commands on externally running process

s-baumann
Sat, 30 Jul 2022 11:53:18 +0000
General Usage

I want to run one command to open a program. Then do a second command in this program.
For instance in the below case the first line opens a psql session and the second should insert some data via a csv file.

line1 = `$LOCAL_POSTGRES_INSTALLATION -h $HOST -U $DATABASE_USER -d $DBNAME -p $PORT`
line2= ` \\COPY $schema_name.$table_name \( $DATABASE_COLUMNS \) FROM  $FILENAME csv header \; `

I am struggling to do this via a pipeline. The best I have got is:

pstdin = Pipe()
pstdout = Pipe()
pstderr = Pipe()
proc = run(pipeline(line1,
                    stdin = pstdin, stdout = pstdout, stderr = pstderr),
                    wait = false)

If you run process_running(proc) it seems like I have a process running psql. But I am not sure how to then send each command to this running process.

Any ideas how to do this?

The documentation suggests something like:
run(pipeline(line1 , line2));
should send each command in turn but this did not work for me (I guess as second command is run in program started by the first).