To create an Elixir script that connects to a RabbitMQ server, listens for messages on a specific queue, and inserts the data into a PostgreSQL database, you'll need to use several libraries:

1. `amqp` - A RabbitMQ client library for Elixir.
2. `postgrex` - A PostgreSQL driver for Elixir.
3. `jason` - A JSON parser for Elixir.

First, add these dependencies to your `mix.exs` file:

```elixir
defp deps do
  [
    {:amqp, "~> 2.0"},
    {:postgrex, ">= 0.0.0"},
    {:jason, "~> 1.0"}
  ]
end
```

Next, create a new module called `RabbitConsumer` in a file named `rabbit_consumer.ex`. This module will handle connecting to RabbitMQ, listening for messages, parsing JSON payloads, validating data, and inserting records into the PostgreSQL database.

```elixir
defmodule RabbitConsumer do
  require Logger

  alias AMQP.Connection
  alias AMQP.Channel
  alias Postgrex

  @queue_name "your_queue_name"
  @exchange_name "your_exchange_name"

  def start_link() do
    # Start the process
    Task.start_link(fn -> run() end)
  end

  defp run() do
    with {:ok, conn} <- Connection.open(),
         {:ok, chan} <- Channel.open(conn),
         :ok <- setup_queue(chan) do
      consume_messages(chan)
    else
      error ->
        Logger.error("Failed to connect or set up queue: #{inspect(error)}")
        Process.sleep(5_000)
        run()
    end
  end

  defp setup_queue(channel) do
    with :ok <- Channel.queue_declare(channel, @queue_name),
         :ok <- Channel.exchange_declare(channel, @exchange_name, :direct),
         :ok <- Channel.queue_bind(channel, @queue_name, @exchange_name) do
      {:ok, channel}
    end
  end

  defp consume_messages(channel) do
    Basic.consume(channel, @queue_name)
    |> handle_response()
  end

  defp handle_response({:basic_deliver, payload, meta}) do
    with {:ok, json} <- Jason.decode(payload),
         {:ok, data} <- validate_data(json),
         {:ok, _result} <- insert_into_db(data) do
      Logger.info("Message processed successfully")
    else
      error ->
        Logger.error("Failed to process message: #{inspect(error)}")
    end

    # Continue consuming messages
    consume_messages(channel)
  end

  defp validate_data(data) do
    # Perform your validation logic here
    # Return {:ok, data} if the data is valid, or {:error, reason} otherwise
    {:ok, data}
  end

  defp insert_into_db(data) do
    with {:ok, pid} <- Postgrex.start_link(database: "your_database", username: "your_username", password: "your_password"),
         {:ok, result} <- Postgrex.query(pid, "INSERT INTO your_table (column1, column2) VALUES ($1, $2)", [data["key1"], data["key2"]]) do
      {:ok, result}
    else
      error ->
        Logger.error("Failed to insert into database: #{inspect(error)}")
        {:error, error}
    end
  end
end
```

Finally, update your application's `start/2` function in the main module to start the `RabbitConsumer`:

```elixir
def start(_type, _args) do
  children = [
    RabbitConsumer
  ]

  opts = [strategy: :one_for_one, name: YourApp.Supervisor]
  Supervisor.start_link(children, opts)
end
```

Now your Elixir script will connect to a RabbitMQ server, listen for messages on the specified queue, parse JSON payloads, validate data, and insert records into a PostgreSQL database. The script also includes error handling to recover from potential failures.
RabbitMQ 서버에 연결하고, 특정 큐에서 메시지를 수신 대기하고, 데이터를 PostgreSQL 데이터베이스에 삽입하는 Elixir 스크립트를 생성하려면 다음과 같은 여러 라이브러리를 사용해야 합니다.

1. amqp` - Elixir용 RabbitMQ 클라이언트 라이브러리입니다.
2. `postgrex` - Elixir용 PostgreSQL 드라이버.
3. jason` - Elixir용 JSON 파서.

먼저, 이러한 종속성을 `mix.exs` 파일에 추가하세요:

```elixir
defp deps do
  [
    {:amqp, "~> 2.0"},
    {:postgrex, ">= 0.0.0"},
    {:jason, "~> 1.0"}
  ]
end
```

다음으로, `rabbit_consumer.ex`라는 파일에 `RabbitConsumer`라는 새 모듈을 생성합니다. 이 모듈은 RabbitMQ에 연결하고, 메시지를 수신하고, JSON 페이로드를 구문 분석하고, 데이터 유효성을 검사하고, PostgreSQL 데이터베이스에 레코드를 삽입하는 작업을 처리합니다.

```elixir
defmodule RabbitConsumer do
  require Logger

  alias AMQP.Connection
  alias AMQP.Channel
  alias Postgrex

  @queue_name "your_queue_name"
  @exchange_name "your_exchange_name"

  def start_link() do
    # Start the process
    Task.start_link(fn -> run() end)
  end

  defp run() do
    with {:ok, conn} <- Connection.open(),
         {:ok, chan} <- Channel.open(conn),
         :ok <- setup_queue(chan) do
      consume_messages(chan)
    else
      error ->
        Logger.error("Failed to connect or set up queue: #{inspect(error)}")
        Process.sleep(5_000)
        run()
    end
  end

  defp setup_queue(channel) do
    with :ok <- Channel.queue_declare(channel, @queue_name),
         :ok <- Channel.exchange_declare(channel, @exchange_name, :direct),
         :ok <- Channel.queue_bind(channel, @queue_name, @exchange_name) do
      {:ok, channel}
    end
  end

  defp consume_messages(channel) do
    Basic.consume(channel, @queue_name)
    |> handle_response()
  end

  defp handle_response({:basic_deliver, payload, meta}) do
    with {:ok, json} <- Jason.decode(payload),
         {:ok, data} <- validate_data(json),
         {:ok, _result} <- insert_into_db(data) do
      Logger.info("Message processed successfully")
    else
      error ->
        Logger.error("Failed to process message: #{inspect(error)}")
    end

    # Continue consuming messages
    consume_messages(channel)
  end

  defp validate_data(data) do
    # Perform your validation logic here
    # Return {:ok, data} if the data is valid, or {:error, reason} otherwise
    {:ok, data}
  end

  defp insert_into_db(data) do
    with {:ok, pid} <- Postgrex.start_link(database: "your_database", username: "your_username", password: "your_password"),
         {:ok, result} <- Postgrex.query(pid, "INSERT INTO your_table (column1, column2) VALUES ($1, $2)", [data["key1"], data["key2"]]) do
      {:ok, result}
    else
      error ->
        Logger.error("Failed to insert into database: #{inspect(error)}")
        {:error, error}
    end
  end
end
```

마지막으로 메인 모듈에서 애플리케이션의 `start/2` 함수를 업데이트하여 `RabbitConsumer`를 시작하세요:

```elixir
def start(_type, _args) do
  children = [
    RabbitConsumer
  ]

  opts = [strategy: :one_for_one, name: YourApp.Supervisor]
  Supervisor.start_link(children, opts)
end
```

이제 Elixir 스크립트가 RabbitMQ 서버에 연결하여 지정된 대기열에서 메시지를 수신하고, JSON 페이로드를 구문 분석하고, 데이터의 유효성을 검사하고, PostgreSQL 데이터베이스에 레코드를 삽입합니다. 스크립트에는 잠재적인 장애로부터 복구하기 위한 오류 처리 기능도 포함되어 있습니다.