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.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: discord-to-postgresql-0.1.2.tar.gz
  • Upload date:
  • Size: 9.0 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.2.tar.gz
Algorithm Hash digest
SHA256 b3c5a4f363ad92b83e09adb59601399d61cedec016bc80c914401ca1f089c17b
MD5 40f6068f88a7fce4dfb4c3726fb25727
BLAKE2b-256 e28d28485b2a3f35f575588641075ed32766d039d4a08ff85b2af34df73a052b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for discord_to_postgresql-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 04046c97c6a2f0db8c88afda6dbca2b0cf82eeae8a8d9f377bcb48fcf7334655
MD5 6bdf3ce0e69fa659c10922dec76e8031
BLAKE2b-256 190e6630e3ef7321effbd894a9072785193d71dc149f0074cf82396301e86878

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