Import Users via CSV

I know there is a ‘frontend’ way to invite users via CSV, however that will not work in my case, as it will send an email to the thousands of people who will be added to the site.

When trying to add it via the CLI, I get the following errors.

/var/www/discourse# rails runner script/import_scripts/csv_importer.rb /var/www/discourse/tmp/users.csv
Loading existing groups…
Loading existing users…
Loading existing categories…
Loading existing posts…
Loading existing topics…
File doesn’t exist: /var/www/discourse/tmp/emails.csv
File doesn’t exist: /var/www/discourse/tmp/custom_fields.csv
bundler: failed to load command: script/rails (script/rails)
/var/www/discourse/script/import_scripts/csv_importer.rb:29:in initialize': undefined method headers’ for nil (NoMethodError)

@imported_custom_fields_names = @imported_custom_fields.headers.drop(1)
                                                       ^^^^^^^^

Any thoughts on what could be going on?

This script is a migration script, it’s not intended for doing the singular task of adding users.

Do you just want to create a larger number of real user accounts, or are they for testing?

Aha! I saw importer, not migration! Thanks for the tip.

I want to import a large amount of users via CSV

Import and migrate here are mostly synonyms.

It should do the job of creating all of those users (they won’t have passwords, so they’ll need to get one, probably by having an email sent to them).

You’ll likely need to understand a bit about programming to make it work.

First off - happy birthday.

I have next to no programming skills. I assumed it would be as simple as kicking off the import.rb file ‘pointed’ at the users.csv.

The errors it output make exactly zero sense to me. I looked at line 29 of the import script and it referenced headers, so I assumed it meant the columns of the csv file, so I spent some time mucking with that, to no avail.

Well, you didn’t get very far since it never even opened your csv.

It’s one of those frustrating things where it could take 30 seconds or hours.

But you might try copying the file to there. It’s tricky because it’s one place in the file system of the computer and another inside the container. Once you solve that you can start mucking with the column headings.

Sometimes there’s an easy solution to the original problem[1].

Disable emails, import the users, then re-enable emails.


  1. see x-y problem ↩︎

לייק 1

Aha - but the issue here, is that I think the importer picked up the users.csv.

I don’t have or want to import emails.csv etc.

Or is this a multipart importer? Where users (names) are in one csv, and the amils are in another csv (emails.csv)

It looks like it is finding the users.csv file as it is not barfing an error about that one.

I have not looked at the code lately, but users csv needs to have the email address. The topics and posts are on another csv.

OK, so I am in the right track. I will keep plugging away at this.

Here’s where the filename gets set:

  CSV_FILE_PATH = ENV["CSV_USER_FILE"] || "/var/www/discourse/tmp/users.csv"

So either put the file there, or set ENV variable CSV_USER_FILE to wherever you put the file.

Oh, but the first thing the script does is read in ALL of the CSVs it expects (all of those @… = load_csv lines), so you’ll need to comment out or delete the ones you don’t want. And the import_users looks at custom field data, so you need to change that too.

If that’s not enough of a hint, and you’ve got a budget, email (in my profile) me a number and I’ll see what I can find a way to help you for that number.

לייק 1