"Manually" create new users

Hey there,
I’m using Ghost and I find it very user-friendly and powerful, it’s a very well-designed product.
One thing I don’t understand is how I can “manually” create users, in the sense that I want to insert records in the database without using the “invite” button.
I know it’s not recommended but a customer asked if I could implement it (and strictly requires something like this).

I already came up with this solution:
INSERT INTO users (id, name, slug, password, email, created_at, created_by) VALUES(...)
and then I would insert the id in the roles_users table.
I noticed (as mentioned in this topic) that Ghost outputs Validation (matches) failed for id as soon as I try do edit something because I’m not generating a valid ObjectID.
What exactly is an ObjectID?
How can I generate it?

Thanks in advance.

2 Likes

So to be clear, it’s not recommended to modify the database directly, if you’re self hosting you might be better off adding/changing at the api level in order to create users via the model.

However, if you do need to do it this way, you can use bson-objectid - npm to generate ObjectID’s :slight_smile:

1 Like

Thanks a lot, really appreciate it.
Keep up the good work with Ghost!

1 Like

It’s probably too late now, but in case someone stumbles on this thread later, here’s my solution. I basically created a Ghost import file with users only, using the following format:

{
  "data": {
    "posts": [],
    "tags": [],
    "posts_tags": [],
    "users": [
      {
        "id": 1,
        "slug": "person",
        "bio": "I am a person on the website",
        "website": "https://www.example.com",
        "created_at": "Thu, 3 Aug 2019 13:45:57 +0530",
        "created_by": 1,
        "email": "person@example.com",
        "name": "Some Person",
        "profile_image": ""
      }
    ]
  },
  "meta": {
    "exported_on": "Thu, 3 Aug 2019 13:46:48 +0530",
    "version": "2.14.0"
  }
}

…with the following changes:

  • Replace the slug, bio, website, email and name with preferred values for the person (I think they’re optional apart from email)
  • Increment the id for each new record (IDs have to be unique to the file, but not necessarily to the database since Ghost will automatically assign an ObjectID before importing)
  • Change the date and time to the current date and time–you can do this manually, the format’s pretty straightforward. The +0530 at the end is my timezone, UTC +5:30; you can change it to your own if it really matters
  • Change the date and time for the meta.exported_on as well

When it’s all done, go to the “Labs” section of Ghost and run “import content”. And then voilà–your user will appear in the database!


You can probably save this as a template for importing more users in the future. And of course, if you want to import multiple users at a time, you can just duplicate the "users" object to add more people (don’t forget commas in between!). It’d be pretty straightforward to write a script automating the process, since it’s basically plaintext.

6 Likes

Hi, be careful about id field is a object id now.
The I’d 1 is a legacy by a type of super user
To insert other users youul need generate a objectId

An important point, but not in this case: the Ghost import file can use integers as id: Ghost automatically generates ObjectIDs for each user as it creates it ignoring the id field.

For imports, the id field is only used to reference users within the import file (eg. linking a user to a post). If no matching user is found in the import file, Ghost defaults to using the superuser so there’s no risk of accidentally risking to some random existing user.

1 Like

For anyone stumbling on this thread later: I’ve made a quick tool to generate this JSON file so you don’t have to do it by hand:

https://ghost-user-maker.code.snipettemag.com/

This was mainly made for internal use (for my team to “manually” add authors to Ghost) but anyone else is welcome to use it :slightly_smiling_face:

After filling in the details, you’ll get a JSON file to download which you can then import by opening your Ghost settings and going to Settings → Labs → Import content.

3 Likes

If I have lot of content already, and I am specifically importing the above content, will it replace the existing content?
Or should I export first and add the user to json and reupload?