Copy output of sql-query to a file

Copy output of sql-query to a file

I want to export a random entry of my database into a file with the command

I want to export a random entry of my database into a file with the command

SELECT * FROM my_table ORDER BY RANDOM() LIMIT 1 \g /path/file;

This query works if I enter it in my db terminal, but I want to us this query with a bash script but then I get the error: syntax error at or near "\g"

My bash script looks like this:

 PGPASSWORD=*** psql -U user -d db_name -h localhost -p port -t -c "SELECT * FROM my_table ORDER BY RANDOM() LIMIT 1 \g /path/file"


bash postgresql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Bash Select (Make Menus) and Bash select Example

In this tutorial, we will cover the basics of the select construct in Bash. The select construct generates a menu from a list of items. It has almost the same syntax as for loop.If the user enters a number that corresponds to the number of one of the displayed items, then the value of [ITEM] is set to that item. The value of the selected item is stored in the variable REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again.

Bash until Loop and Bash until Loop Example

This tutorial explains the basics of the until loop in Bash. The until loop is used to execute a given set of commands as long as the given condition evaluates to false.

Bash For Loop,The Standard Bash for Loop

In this tutorial, we will cover the basics of for loops in Bash. We will also show you how to use the break and continue statements to alter the flow of a loop.

Bash Functions,Defining Bash Functions

A Bash function is essentially a set of commands that can be called numerous times. The purpose of a function is to help you make your bash scripts more readable and to avoid writing the same repeatedly.

Bash break and continue

Loops allow you to run one or more commands multiple times until a certain condition is met. In Bash, break and continue statements allows you to control the loop execution.