# Importing users from an external database

**URL:** https://meta.discourse.org/t/importing-users-from-an-external-database/49754
**Category:** Support
**Created:** [September 5, 2016, 9:35pm UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754 "2016-09-05T21:35:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kgish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kgish/32/121131_2.png) [@kgish](https://meta.discourse.org/u/kgish)
#### Post date: [September 5, 2016, 9:35pm UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/1 "2016-09-05T21:35:05Z")

</div>

I have a list of users from an external database which i need to import into discourse and activate them. How should i do this?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [September 5, 2016, 10:19pm UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/2 "2016-09-05T22:19:09Z")

</div>

You could feed the emails into the invites tab on your user page.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [September 6, 2016, 5:28am UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/3 "2016-09-06T05:28:29Z")

</div>

And if you don’t want a one-off import, but a continuous “sync”, search for SSO 🙂

---

<div class="post-metadata">

### Author: ![kgish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kgish/32/121131_2.png) [@kgish](https://meta.discourse.org/u/kgish)
#### Post date: [September 12, 2016, 11:48am UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/4 "2016-09-12T11:48:59Z")

</div>

For those interested, here’s how I did it:

```plaintext
desc "Import users from a CSV file"
task :import, [:csv_file] => [:environment] do |_, args|
  cnt = 0
  CSV.foreach(args[:csv_file], col_sep: ';', headers: true) do |new_user|
    cnt = cnt + 1
    new_user = define_user(new_user, cnt)
    u = User.new({
      username: new_user['username']
      email: new_user['email'],
      password: SecureRandom.hex,
      name: new_user['name'],
      title: new_user['title'],
      approved: true,
      approved_by_id: -1,
      trust_level: 1
    })
    u.import_mode = true
    u.groups = parse_user_groups new_user['groups']

    begin
      u.save!
    rescue => e
      puts "NOK|#{cnt}|#{new_user['name']}|#{new_user['username']}|#{new_user['email']}|#{new_user['groups']} => #{e.message}"
    else
      token = ActiveRecord::Base.connection.execute("select * from email_tokens where user_id=#{u.id}")
      puts "OK |#{cnt}|#{new_user['name']}|#{new_user['username']}|#{new_user['email']}|#{new_user['groups']}|#{token[0]['token']}"
    end
end

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [March 5, 2019, 1:28pm UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/5 "2019-03-05T13:28:06Z")

</div>



---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [March 6, 2019, 11:27am UTC](https://meta.discourse.org/t/importing-users-from-an-external-database/49754/6 "2019-03-06T11:27:51Z")

</div>


