Skip to content

Commit 688f470

Browse files
add optional postgresql support for stellar core
1 parent 54cfdb8 commit 688f470

File tree

7 files changed

+53
-8
lines changed

7 files changed

+53
-8
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ curl "http://localhost:11626/ll?level=DEBUG&partition=SCP"
205205

206206
Available partitions: `Fs`, `SCP`, `Bucket`, `Database`, `History`, `Process`, `Ledger`, `Overlay`, `Herder`, `Tx`, `LoadGen`, `Work`, `Invariant`, `Perf`
207207

208+
### Core Database Options
209+
210+
By default, Stellar Core uses SQLite for its database. To use PostgreSQL instead, set the `CORE_USE_POSTGRES` environment variable:
211+
212+
```shell
213+
docker run -e CORE_USE_POSTGRES=true -p "8000:8000" stellar/quickstart --local
214+
```
215+
216+
When `CORE_USE_POSTGRES=true`:
217+
- A `core` PostgreSQL database is created alongside the `horizon` database
218+
- Stellar Core connects to PostgreSQL instead of SQLite
219+
- PostgreSQL is started before Stellar Core
220+
221+
> [!WARNING]
222+
> PostgreSQL support for Stellar Core in Quickstart is deprecated and will likely be removed in a future release. It is highly recommended not to use this feature if you are not already using PostgreSQL. If you are currently using PostgreSQL for Stellar Core in Quickstart, please comment on [this issue](https://github.com/stellar/quickstart/issues/875) with information about your use case for which PostgreSQL is necessary.
223+
208224
### Stellar Lab
209225

210226
Stellar Lab is an interactive toolkit for exploring and interacting with the Stellar network. It allows developers to build, sign, simulate, and submit transactions, and to make requests to both the Friendbot, RPC, and Horizon APIs. Lab is also built-in to Quickstart.
@@ -495,7 +511,7 @@ The point of this project is to make running stellar's software within your own
495511

496512
This image manages a postgres database `horizon` for horizon's data. The username to use when connecting with your postgresql client or library is `stellar`. The password to use is dependent upon the mode your container is running in: Persistent mode uses a password supplied by you and ephemeral mode generates a password and prints it to the console upon container startup.
497513

498-
Note: In the past, stellar-core also used PostgreSQL with a `core` database. Stellar-core now uses SQLite, so PostgreSQL is only started when Horizon is enabled.
514+
Note: By default, stellar-core uses SQLite. To use PostgreSQL for stellar-core, set `CORE_USE_POSTGRES=true` (see [Core Database Options](#core-database-options)). When using PostgreSQL, a `core` database is also created.
499515

500516
## Example launch commands
501517

common/core/bin/start

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ if [ -f /opt/stellar/core/etc/env ]; then
55
source /opt/stellar/core/etc/env
66
fi
77

8+
# Wait for postgres if using postgres for core database
9+
if [ "$CORE_USE_POSTGRES" = "true" ]; then
10+
while ! psql -U stellar -c 'select 1' core &> /dev/null ; do
11+
echo "Waiting for postgres to be available..."
12+
sleep 1
13+
done
14+
fi
15+
816
echo "starting core..."
917
set -e
1018
exec /usr/bin/stellar-core --conf "/opt/stellar/core/etc/stellar-core.cfg" run ${CORE_LOG_LEVEL:+--ll "$CORE_LOG_LEVEL"}

futurenet/core/etc/stellar-core.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LOG_FILE_PATH="/var/log/stellar-core/stellar-core-{datetime:%Y-%m-%d_%H-%M-%S}.l
44
MANUAL_CLOSE=__MANUAL_CLOSE__
55

66
NETWORK_PASSPHRASE="__NETWORK__"
7-
DATABASE="sqlite3:///opt/stellar/core/stellar.db"
7+
DATABASE="__DATABASE__"
88
UNSAFE_QUORUM=true
99
FAILURE_SAFETY=0
1010
CATCHUP_RECENT=100

local/core/etc/stellar-core.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ NETWORK_PASSPHRASE="__NETWORK__"
1111
NODE_SEED="SDQVDISRYN2JXBS7ICL7QJAEKB3HWBJFP2QECXG7GZICAHBK4UNJCWK2 self"
1212
NODE_IS_VALIDATOR=true
1313

14-
DATABASE="sqlite3:///opt/stellar/core/stellar.db"
14+
DATABASE="__DATABASE__"
1515

1616
FAILURE_SAFETY=0
1717
UNSAFE_QUORUM=true

pubnet/core/etc/stellar-core.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PUBLIC_HTTP_PORT=true
33
LOG_FILE_PATH="/var/log/stellar-core/stellar-core-{datetime:%Y-%m-%d_%H-%M-%S}.log"
44
MANUAL_CLOSE=__MANUAL_CLOSE__
55

6-
DATABASE="sqlite3:///opt/stellar/core/stellar.db"
6+
DATABASE="__DATABASE__"
77
NETWORK_PASSPHRASE="Public Global Stellar Network ; September 2015"
88
CATCHUP_RECENT=100
99

start

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export PROTOCOL_VERSION_DEFAULT="$(< /image.json jq -r '.config.protocol_version
4343
: "${ENABLE_RPC_ADMIN_ENDPOINT:=$ENABLE_SOROBAN_RPC_ADMIN_ENDPOINT}"
4444
: "${ENABLE_CORE_MANUAL_CLOSE:=false}"
4545
: "${CORE_LOG_LEVEL:=}"
46+
: "${CORE_USE_POSTGRES:=}"
4647
: "${LIMITS:=testnet}"
4748

4849
QUICKSTART_INITIALIZED=false
@@ -409,7 +410,8 @@ function copy_pgpass() {
409410
}
410411

411412
function init_db() {
412-
if [ "$ENABLE_HORIZON" != "true" ]; then
413+
# Need postgres if horizon is enabled, or if core is enabled and using postgres
414+
if [ "$ENABLE_HORIZON" != "true" ] && { [ "$ENABLE_CORE" != "true" ] || [ "$CORE_USE_POSTGRES" != "true" ]; }; then
413415
return 0
414416
fi
415417
if [ -f $PGHOME/.quickstart-initialized ]; then
@@ -441,10 +443,16 @@ function init_db() {
441443
run_silent "init-postgres" sudo -u postgres $PGBIN/initdb -D $PGDATA
442444

443445
start_postgres
444-
run_silent "create-horizon-db" sudo -u postgres createdb horizon
446+
if [ "$ENABLE_HORIZON" = "true" ]; then
447+
run_silent "create-horizon-db" sudo -u postgres createdb horizon
448+
fi
449+
if [ "$ENABLE_CORE" = "true" ] && [ "$CORE_USE_POSTGRES" = "true" ]; then
450+
run_silent "create-core-db" sudo -u postgres createdb core
451+
fi
445452
run_silent "stellar-postgres-user" sudo -u postgres psql <<-SQL
446453
CREATE USER $PGUSER WITH PASSWORD '$PGPASS';
447-
GRANT ALL PRIVILEGES ON DATABASE horizon to $PGUSER;
454+
$([ "$ENABLE_HORIZON" = "true" ] && echo "GRANT ALL PRIVILEGES ON DATABASE horizon to $PGUSER;")
455+
$([ "$ENABLE_CORE" = "true" ] && [ "$CORE_USE_POSTGRES" = "true" ] && echo "GRANT ALL PRIVILEGES ON DATABASE core to $PGUSER;")
448456
SQL
449457

450458
touch .quickstart-initialized
@@ -471,6 +479,7 @@ function init_stellar_core() {
471479
fi
472480
cat > $COREHOME/etc/env <<EOF
473481
CORE_LOG_LEVEL="$core_log_level"
482+
CORE_USE_POSTGRES="$CORE_USE_POSTGRES"
474483
EOF
475484

476485
if [ -f $COREHOME/.quickstart-initialized ]; then
@@ -487,6 +496,15 @@ EOF
487496

488497
run_silent "finalize-core-config-manual-close" perl -pi -e "s/__MANUAL_CLOSE__/$ENABLE_CORE_MANUAL_CLOSE/g" etc/stellar-core.cfg
489498

499+
# Set database based on CORE_USE_POSTGRES environment variable
500+
if [ "$CORE_USE_POSTGRES" = "true" ]; then
501+
local DATABASE_URL="postgresql://dbname=core host=localhost user=stellar password=$PGPASS"
502+
start_postgres
503+
else
504+
local DATABASE_URL="sqlite3:///opt/stellar/core/stellar.db"
505+
fi
506+
run_silent "finalize-core-config-database" perl -pi -e "s|__DATABASE__|$DATABASE_URL|g" etc/stellar-core.cfg
507+
490508
run_silent "init-core-db" sudo -u stellar stellar-core new-db --conf etc/stellar-core.cfg
491509

492510
if [ "$NETWORK" = "local" ]; then
@@ -700,6 +718,9 @@ function start_optional_services() {
700718
done
701719

702720
if [ "$ENABLE_CORE" == "true" ]; then
721+
if [ "$CORE_USE_POSTGRES" == "true" ]; then
722+
supervisorctl start postgresql
723+
fi
703724
supervisorctl start stellar-core
704725
fi
705726

testnet/core/etc/stellar-core.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LOG_FILE_PATH="/var/log/stellar-core/stellar-core-{datetime:%Y-%m-%d_%H-%M-%S}.l
44
MANUAL_CLOSE=__MANUAL_CLOSE__
55

66
NETWORK_PASSPHRASE="__NETWORK__"
7-
DATABASE="sqlite3:///opt/stellar/core/stellar.db"
7+
DATABASE="__DATABASE__"
88
CATCHUP_RECENT=100
99

1010
UNSAFE_QUORUM=true

0 commit comments

Comments
 (0)