Using raster2pgsql in Python console of QGIS?

Here's a script that iterates on all the tiff rasters in a folder and creates a table with auto tile size for each (based on this link):

import psycopg2
import subprocess 
import sys, os

input_path = " C:\\qgis_cloud_data\\"
#Change to the location of pgsql bin folder
os.environ['PATH'] = r';C:\pgsql\9.6\bin'
os.environ['PGHOST'] = 'localhost'
os.environ['PGPORT'] = '9008'
os.environ['PGUSER'] = 'postgres'
os.environ['PGPASSWORD'] = 'dbpass'
os.environ['PGDATABASE'] = 'dbname'

for raster in os.listdir(input_path):
    if raster.endswith(".tif"):
       name = raster.split(".tif")[0]
       # Print the foound tiff name
       print(name)     
       raster = os.path.join(input_path, raster)                    
       # Print the full path of the tiff raster
       print(raster)
       rastername = str(name)
       rasterlayer = rastername.lower()
       conn = psycopg2.connect(database="dbname", user="postgres", host="localhost", password="dbpass", port=9008)
       cursor = conn.cursor()
       cmds = 'raster2pgsql -s 3857 -t auto "' + raster + '" |psql'
       subprocess.call(cmds, shell=True)

+1 for NettaB's answer but just wanted to add that if all of your tiff files are in the same folder you should be able to accomplish this using a single command (be it from the command line or via subprocess in Python):

# Set environment variables for database connection
set PGHOST=db.qgiscloud.com
set PGPORT=5432
set PGUSER=enter_qgiscloud_user
set PGPASSWORD=enter_qgiscloud_pw
set PGDATABASE=enter_qgiscloud_db

# Call the raster2pqsql utility
raster2pgsql -s 3857 -C -F -t auto C:/qgis_cloud_data/*.tif schema.target_table | psql

This will create a new table named schema.target_table and push the data into it. If you need some more info on the switches to use, this page is useful - and includes some examples.

With the Python implementation you don't need to use Psycopg unless you plan to execute SQL queries - if you're just loading the data straight in you only need the raster2pgsql utility. So your code can be adapted to:

import os
import subprocess

db_name = 'enter_qgiscloud_db'
db_host = 'db.qgiscloud.com'
db_user = 'enter_qgiscloud_user'
db_password = 'enter_qgiscloud_pw'

# Set pg password environment variable - others can be included in the statement
os.environ['PGPASSWORD'] = db_password 

# Build command string
cmd = 'raster2pgsql -s 3857 -C -F -t auto C:/qgis_cloud_data/*.tif schema.target_table | psql -U {} -d {} -h {} -p 5432'.format(db_user,db_name,db_host)

# Execute
subprocess.call(cmd, shell=True)