Skip to main content

Imports Discord messages archive to PostgreSQL database

Project description

Discord to PostgreSQL

Imports Discord messages archive to PostgreSQL database.

Tested in Python 3.7, 3.8, and 3.9 on Linux and Windows.

Installation

pip3 install discord-to-postgresql

Usage

Run with postgresql credentials as arguments

discord_to_postgresql path/to/package.zip replace myusername mypassword 0.0.0.0 5432 postgres

Run with textfile containing PostgreSQL connection URL specified as argument

discord_to_postgresql path/to/package.zip replace -t path/to/conn_url.txt

Sample contents of textfile

postgresql://myusername:mypassword@0.0.0.0:5432/postgres

PostgreSQL output tables

discord_user

Column Type
id bigint
username character varying
discriminator character varying

discord_server

Column Type
id bigint
name character varying

discord_channel

type is the "type" of the discord channel. Known possible values of type are:

  • 0 = normal server channel
  • 1 = direct message (between 2 people only)
  • 3 = group chat (may be more than 2 people)
  • 5 = announcement channel
  • 11 = thread
Column Type
id bigint
type integer
name character varying
server_id bigint

discord_recipient

A private chat will have 2 "recipients" (members), or 1-8 for group chats.

Column Type
channel_id bigint
user_id bigint

discord_message

Column Type
id bigint
timestamp timestamp without time zone
contents text
attachments character varying
channel_id bigint
user_id bigint

Command-line arguments

  • --package: package.zip file path.
  • --if_exists: How to behave if the tables already exist. This argument will be directly passed to the DataFrame.to_sql() function. Possible values:
    • fail: Raise a ValueError.
    • replace: Drop the table before inserting new values.
    • append: Insert new values to the existing table.
  • --username: PostgreSQL server username. Optional if text_file_with_url already specified.
  • --password: PostgreSQL server password. Optional if text_file_with_url already specified.
  • --host: PostgreSQL server host URL (e.g. localhost or xxx.xxx.xxx.xxx). Optional if text_file_with_url already specified.
  • --port: PostgreSQL server port. Optional if text_file_with_url already specified.
  • --db_name: PostgreSQL server database name. Optional if text_file_with_url already specified.
  • --text_file_with_url: Text file containing PostgreSQL DB connection URL. Optional if username, password, host, port, db_name are all specified. URL is of the format: postgresql://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DB_NAME>

Addendum: Easiest way to start up a test PostgreSQL server

Install Docker: https://docs.docker.com/engine/install/

Then type this command to install a Docker PostgreSQL image and run as a container:

docker run -p 5432:5432 --name test-postgres -e POSTGRES_USER=myusername -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=postgres -d postgres

This will run a postgresql server accessible on localhost:5432 with username myusername and password mypassword, and create a databased named postgres.

To stop the container:

docker stop test-postgres

To restart the container:

docker start test-postgres

To delete the container (and erase all db data):

docker rm test-postgres

after which it is completely fine to run the above-mentioned docker run command again.

Heroku Postgres (free cloud server) is another option but has a maximum row number limit.

TODO

  • Test for other versions of Python
  • Test for Windows
  • Add support for primary keys (no duplicate rows if append)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord-to-postgresql-0.1.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

discord_to_postgresql-0.1.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file discord-to-postgresql-0.1.1.tar.gz.

File metadata

  • Download URL: discord-to-postgresql-0.1.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for discord-to-postgresql-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b281ed850374eb842b490fe1b68b0edcd3623ff6ec43fb47b5d02e5f0d423612
MD5 76c51f2cd3e73382f31afaf0a7ffd941
BLAKE2b-256 fe1de5282a0ce356f06bc5a50d3e618cddd9c805d110bbf262c602297ace12dd

See more details on using hashes here.

File details

Details for the file discord_to_postgresql-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_to_postgresql-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d929ddeabb7f957797a680e106d5ccc7662b824e1129f1568102ec68db9a90fb
MD5 0bdcfa0280b75d1b9822b08b1b0e2772
BLAKE2b-256 1d59af13351e4af9f3c07469b1d5f5bb4cbffaf48ae5d0b2f3bba48121b661c6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page