How to Run PostgreSQL Server and Database?
1. Online Tutorials
2. Issues
2.1
psycopg2.OperationalError: could not connect to server: Connection refused
Relevant resource:
- Connection refused with postgresql using psycopg2
- Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails
Solution:
Just modify this file
/etc/postgresql/12/main/pg_hba.conf
:
local all postgres peer
=>local all postgres trust
local all all peer
=>local all all md5
2.2
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
Relevant resource:
- Postgres could not connect to server
- Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
- psql: could not connect to server: No such file or directory (Mac OS X)
- How to change the default unix socket location from /tmp for PostgreSQL
Solution:
- find the folder containing the file
postmaster.pid
, and remove it:rm /var/lib/postgresql/12/main/postmaster.pid
- edit file
/etc/postgresql/12/main/postgresql.conf
, in which, set:listen_addresses = '*'
port = 5432
unix_socket_directory = '/var/run/postgresql'
2.3 CREATE EXTENSION in PostgreSQL
Relevant resource:
- How do I import modules or install extensions in PostgreSQL 9.1+?
- Using psql how do I list extensions installed in a database?
Solutions:
- Please make sure
postgresql-contrib
has been installed.
2.4 Deal With GinIndex
Relevant resource:
- How to create GIN index in Django migration
- What is gin_trgm_ops of PostgreSQL?
- gin_trgm_ops does not exist for access method "gin"
- Creating a Gin Index with Trigram (gin_trgm_ops) in Django model
- A Guide to Create User-Defined Extension Modules to Postgres
Solutions:
- Please make sure
btree_gin
andpg_trgm
EXTENSIONs have been CREATEed for the user username in PostgreSQL database. Namely, you need to\c database username
beforeCREATE EXTENSION btree_gin
andCREATE EXTENSION pg_trgm
.
2.5
django.db.utils.ProgrammingError: syntax error at or near "FUNCTION"
Solutions:
- Use PROCEDURE instead of FUNCTION.
2.6
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration site.0001_initial is applied before its dependency account.0003_initial on database 'default'.
Relevant resource:
Solutions:
- Please make sure
\c database username
is run from withinpsql
.
2.7
LOG: SSL is not supported by this build
Relevant resource:
Solutions:
1 | ⋊> ~ sudo su - postgres |
3. Process
Step 1.
Install postgresql
and postgresql-contrib
1 | ⋊> ~ apt list --installed | grep postgresql 22:46:41 |
Step 2. Open a FIRST terminal:
1 | ⋊> ~ sudo su - postgres 22:03:23 |
Step 3. Open a SECOND terminal:
- with user postgres,
psql
will do - with other users, such as longervision, you've got
to do
psql -U postgres
1 | ⋊> ~ psql -U postgres 22:06:18 |
Step 4. Saleor Reset Migrations (Optional)
Refer to How to Reset Migrations
Run the following 2 lines:
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
Modify
saleor/settings.py
From to 1
2
3DATABASE_CONNECTION_DEFAULT_NAME: dj_database_url.config(
default="postgres://saleor:saleor@localhost:5432/saleor", conn_max_age=600
), correspondingly.1
2
3DATABASE_CONNECTION_DEFAULT_NAME: dj_database_url.config(
default="postgres://username:password@localhost:5432/database", conn_max_age=600
),
Start Migration
1 | ⋊> /v/w/v/saleor on main ⨯ python manage.py makemigrations 22:32:00 |
1 | ⋊> /v/w/v/saleor on main ⨯ python manage.py migrate 00:02:56 |