#NEWLINE# def psql_query( query , user = None , host = None , port = None , maintenance_db = None , password = None , runas = None ) : #NEWLINE# #INDENT# "\n    Run an SQL-Query and return the results as a list. This command\n    only supports SELECT statements.  This limitation can be worked around\n    with a query like this:\n\n    WITH updated AS (UPDATE pg_authid SET rolconnlimit = 2000 WHERE\n    rolname = 'rolename' RETURNING rolconnlimit) SELECT * FROM updated;\n\n    CLI Example:\n\n    .. code-block:: bash\n\n        salt '*' postgres.psql_query 'select * from pg_stat_activity'\n    " #NEWLINE# ret = [ ] #NEWLINE# csv_query = 'COPY ({0}) TO STDOUT WITH CSV HEADER' . format ( query . strip ( ) . rstrip ( ';' ) ) #NEWLINE# cmdret = _psql_prepare_and_run ( [ '-v' , 'datestyle=ISO,MDY' , '-c' , csv_query ] , runas = runas , host = host , user = user , port = port , maintenance_db = maintenance_db , password = password ) #NEWLINE# if ( cmdret [ 'retcode' ] > 0 ) : #NEWLINE# #INDENT# return ret #NEWLINE# #UNINDENT# csv_file = StringIO ( cmdret [ 'stdout' ] ) #NEWLINE# header = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for row in csv . reader ( csv_file , delimiter = ',' , quotechar = '"' ) : #NEWLINE# #INDENT# if ( not row ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( not cmdret ) : #NEWLINE# #INDENT# header = row #NEWLINE# continue #NEWLINE# #UNINDENT# ret . append ( dict ( zip ( header , row ) ) ) #NEWLINE# #UNINDENT# return ret